01
SQL CONCEPTSELECT
The SELECT clause defines which columns or expressions you want in the final result.
SELECT name, salary
FROM employees;
FROM
FROM identifies the source table or tables from which the database reads data.
SELECT
SELECT determines which columns or expressions appear in the final result.
Result
The database returns a new result set containing the requested values.
Think of SELECT as:"Which information do I want to see?"
02
SQL CONCEPTWHERE
WHERE filters individual rows before they reach the final result.
SELECT name, salary
FROM employees
WHERE salary > 50000;
Row by row
The condition is evaluated against rows from the source data.
TRUE
Rows for which the condition evaluates to true continue through the query.
FALSE
Rows that do not satisfy the condition are excluded.
Important:WHERE filters rows before GROUP BY and before the final SELECT projection.
03
SQL CONCEPTDISTINCT
DISTINCT removes duplicate combinations from the selected result.
SELECT DISTINCT department
FROM employees;
Without DISTINCT
Repeated values can appear multiple times in the result.
With DISTINCT
Duplicate result combinations are eliminated.
04
SQL CONCEPTORDER BY
ORDER BY sorts the rows in the result according to one or more expressions.
SELECT name, salary
FROM employees
ORDER BY salary DESC;
ASC
Sorts values in ascending order.
DESC
Sorts values in descending order.
Multiple columns
You can specify multiple ordering expressions to define secondary sorting.
05
SQL CONCEPTLIMIT
LIMIT restricts how many rows are returned by the query.
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
Common use:Pagination, previews, top-N queries and result exploration.
06
SQL CONCEPTGROUP BY
GROUP BY divides rows into groups that can be summarized with aggregate functions.
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
Group key
The GROUP BY expression determines which rows belong together.
Aggregate
Functions such as COUNT, SUM, AVG, MIN and MAX can summarize each group.
One row per group
The result normally contains one output row for each group.
07
SQL CONCEPTHAVING
HAVING filters groups after GROUP BY and aggregation.
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
WHERE vs HAVING:WHERE filters individual rows; HAVING filters grouped results.
08
SQL CONCEPTJOIN
JOIN combines rows from multiple tables using a relationship between their columns.
SELECT
employees.name,
departments.department_name
FROM employees
JOIN departments
ON employees.department = departments.department_name;
TABLE Aemployeesdepartment
TABLE Bdepartmentsdepartment_name
INNER JOINMatching rows from both sides.
LEFT JOINAll rows from the left table plus matching rows.
RIGHT JOINAll rows from the right table plus matching rows.
FULL JOINRows from both sides, matched where possible.
09
SQL CONCEPTAGGREGATE FUNCTIONS
Aggregate functions calculate a value from a set of rows.
SELECT
COUNT(*) AS employees,
AVG(salary) AS average_salary,
MAX(salary) AS highest_salary
FROM employees;
COUNTCounts rows or values
SUMAdds numeric values
AVGCalculates an average
MINFinds the minimum
MAXFinds the maximum
10
SQL CONCEPTCASE
CASE creates conditional values inside a query.
SELECT
name,
salary,
CASE
WHEN salary >= 70000 THEN 'Senior'
WHEN salary >= 50000 THEN 'Mid'
ELSE 'Junior'
END AS level
FROM employees;
Think of CASE as:SQL's conditional expression for producing different values based on conditions.
11
SQL CONCEPTSUBQUERIES
A subquery is a query nested inside another SQL statement.
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
Inner query
Produces a value or result used by the outer query.
Outer query
Uses the subquery result to complete the larger operation.
12
SQL CONCEPTCOMMON TABLE EXPRESSIONS
A CTE gives a temporary name to a query result so it can be referenced by the main statement.
WITH high_earners AS (
SELECT *
FROM employees
WHERE salary > 70000
)
SELECT name, salary
FROM high_earners;
Why use CTEs?They can make complex SQL easier to read and structure.
13
SQL CONCEPTWINDOW FUNCTIONS
Window functions calculate across related rows while keeping individual result rows.
SELECT
name,
department,
salary,
RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS department_rank
FROM employees;
PARTITION BY
Divides rows into independent groups for the window operation.
ORDER BY
Defines the ordering used by the window calculation.
Unlike GROUP BY
Window functions do not collapse the original rows into one row per group.
14
SQL CONCEPTINSERT
INSERT adds new rows to a table.
INSERT INTO employees
(name, department, salary)
VALUES
('Aman', 'Engineering', 65000);
INSERT changes data.Unlike SELECT, it is not primarily used to read rows.
15
SQL CONCEPTUPDATE
UPDATE changes values in existing rows.
UPDATE employees
SET salary = 70000
WHERE employee_id = 5;
Be careful with WHERE.Without an appropriate WHERE condition, an UPDATE can affect many or all rows.
16
SQL CONCEPTDELETE
DELETE removes rows from a table.
DELETE FROM employees
WHERE employee_id = 5;
DELETE is destructive.Always understand which rows match the WHERE condition before executing it.
17
SQL CONCEPTCREATE TABLE
CREATE TABLE defines a new table and its columns.
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
name TEXT,
department TEXT,
salary INTEGER
);
Columns
Define the data fields that rows can contain.
Data types
Define the kind of values that each column can store.
Constraints
PRIMARY KEY, FOREIGN KEY and other constraints define rules around the data.
18
SQL CONCEPTALTER TABLE
ALTER TABLE changes the structure of an existing table.
ALTER TABLE employees
ADD COLUMN email TEXT;
ALTER works on structure.It is used to add, modify or remove schema elements depending on the database system.
19
SQL CONCEPTDROP TABLE
DROP TABLE removes a table definition from the database.
DROP is structural and destructive.It removes the table itself, not merely selected rows.
20
SQL CONCEPTNULL
NULL represents the absence of a value and requires special SQL handling.
SELECT *
FROM employees
WHERE department IS NULL;
IS NULL
Use IS NULL when looking for missing values.
IS NOT NULL
Use IS NOT NULL when you need rows containing a value.
Not equal to NULL
NULL does not behave like an ordinary value in comparisons.
PUT IT ALL TOGETHER
From SQL text
to database result.
A query is not magic. It is a sequence of operations that transforms source data into a result.
01PARSEUnderstand SQL syntax
02SOURCEFind tables and data
03FILTERRemove rows that do not match
04COMBINEJoin or group data
05PROJECTProduce requested columns
Visualize a real query→