SQL EXECUTION GUIDE

Don't just run SQL.
Understand what happens.

Explore SQL query execution step by step. Learn what each clause does, how databases process it, and why the order matters.

20+SQL concepts
01Execution model
∞Queries to explore
QUERY EXECUTION
SELECT name, salary
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;
01FROM
→
02WHERE
→
03SELECT
→
04ORDER
→
05RESULT
Result produced4 rows
THE BIG PICTURE

SQL is written one way.
It is logically processed another.

Understanding this distinction makes complex queries much easier to reason about.

YOU WRITE
01SELECT
02FROM
03WHERE
04GROUP BY
05HAVING
06ORDER BY
07LIMIT
DATABASE
PROCESSING
→
LOGICAL ORDER
01FROM
02WHERE
03GROUP BY
04HAVING
05SELECT
06ORDER BY
07LIMIT
01
SQL CONCEPT

SELECT

The SELECT clause defines which columns or expressions you want in the final result.

SQL
SELECT name, salary
FROM employees;
01employees
→
02read rows
→
03choose columns
→
04result

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 CONCEPT

WHERE

WHERE filters individual rows before they reach the final result.

SQL
SELECT name, salary
FROM employees
WHERE salary > 50000;
01table
→
02check condition
→
03keep matching rows
→
04SELECT
→
05result

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 CONCEPT

DISTINCT

DISTINCT removes duplicate combinations from the selected result.

SQL
SELECT DISTINCT department
FROM employees;
01read rows
→
02SELECT column
→
03remove duplicates
→
04result

Without DISTINCT

Repeated values can appear multiple times in the result.

With DISTINCT

Duplicate result combinations are eliminated.

04
SQL CONCEPT

ORDER BY

ORDER BY sorts the rows in the result according to one or more expressions.

SQL
SELECT name, salary
FROM employees
ORDER BY salary DESC;
01read rows
→
02select result
→
03sort
→
04return rows

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 CONCEPT

LIMIT

LIMIT restricts how many rows are returned by the query.

SQL
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
01read
→
02sort
→
03limit rows
→
04result
Common use:Pagination, previews, top-N queries and result exploration.
06
SQL CONCEPT

GROUP BY

GROUP BY divides rows into groups that can be summarized with aggregate functions.

SQL
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
01read rows
→
02create groups
→
03aggregate
→
04result

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 CONCEPT

HAVING

HAVING filters groups after GROUP BY and aggregation.

SQL
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
01rows
→
02GROUP BY
→
03aggregate
→
04HAVING
→
05result
WHERE vs HAVING:WHERE filters individual rows; HAVING filters grouped results.
08
SQL CONCEPT

JOIN

JOIN combines rows from multiple tables using a relationship between their columns.

SQL
SELECT
  employees.name,
  departments.department_name
FROM employees
JOIN departments
  ON employees.department = departments.department_name;
TABLE Aemployeesdepartment
ON
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 CONCEPT

AGGREGATE FUNCTIONS

Aggregate functions calculate a value from a set of rows.

SQL
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 CONCEPT

CASE

CASE creates conditional values inside a query.

SQL
SELECT
  name,
  salary,
  CASE
    WHEN salary >= 70000 THEN 'Senior'
    WHEN salary >= 50000 THEN 'Mid'
    ELSE 'Junior'
  END AS level
FROM employees;
01read row
→
02check WHEN
→
03choose result
→
04return value
Think of CASE as:SQL's conditional expression for producing different values based on conditions.
11
SQL CONCEPT

SUBQUERIES

A subquery is a query nested inside another SQL statement.

SQL
SELECT name, salary
FROM employees
WHERE salary > (
  SELECT AVG(salary)
  FROM employees
);
01inner query
→
02calculate value
→
03outer query
→
04filter rows
→
05result

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 CONCEPT

COMMON TABLE EXPRESSIONS

A CTE gives a temporary name to a query result so it can be referenced by the main statement.

SQL
WITH high_earners AS (
  SELECT *
  FROM employees
  WHERE salary > 70000
)
SELECT name, salary
FROM high_earners;
01define CTE
→
02execute CTE query
→
03temporary result
→
04main query
Why use CTEs?They can make complex SQL easier to read and structure.
13
SQL CONCEPT

WINDOW FUNCTIONS

Window functions calculate across related rows while keeping individual result rows.

SQL
SELECT
  name,
  department,
  salary,
  RANK() OVER (
    PARTITION BY department
    ORDER BY salary DESC
  ) AS department_rank
FROM employees;
01read rows
→
02partition
→
03order window
→
04calculate
→
05keep rows

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 CONCEPT

INSERT

INSERT adds new rows to a table.

SQL
INSERT INTO employees
  (name, department, salary)
VALUES
  ('Aman', 'Engineering', 65000);
01target table
→
02build row
→
03validate
→
04write row
INSERT changes data.Unlike SELECT, it is not primarily used to read rows.
15
SQL CONCEPT

UPDATE

UPDATE changes values in existing rows.

SQL
UPDATE employees
SET salary = 70000
WHERE employee_id = 5;
01find rows
→
02check WHERE
→
03change values
→
04write changes
Be careful with WHERE.Without an appropriate WHERE condition, an UPDATE can affect many or all rows.
16
SQL CONCEPT

DELETE

DELETE removes rows from a table.

SQL
DELETE FROM employees
WHERE employee_id = 5;
01find rows
→
02check condition
→
03remove rows
→
04commit change
DELETE is destructive.Always understand which rows match the WHERE condition before executing it.
17
SQL CONCEPT

CREATE TABLE

CREATE TABLE defines a new table and its columns.

SQL
CREATE TABLE employees (
  employee_id INTEGER PRIMARY KEY,
  name TEXT,
  department TEXT,
  salary INTEGER
);
01parse definition
→
02validate schema
→
03create structure
→
04store metadata

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 CONCEPT

ALTER TABLE

ALTER TABLE changes the structure of an existing table.

SQL
ALTER TABLE employees
ADD COLUMN email TEXT;
01find table
→
02validate change
→
03modify schema
→
04update metadata
ALTER works on structure.It is used to add, modify or remove schema elements depending on the database system.
19
SQL CONCEPT

DROP TABLE

DROP TABLE removes a table definition from the database.

SQL
DROP TABLE employees;
01find table
→
02check dependencies
→
03remove structure
→
04update metadata
DROP is structural and destructive.It removes the table itself, not merely selected rows.
20
SQL CONCEPT

NULL

NULL represents the absence of a value and requires special SQL handling.

SQL
SELECT *
FROM employees
WHERE department IS NULL;
01read rows
→
02test NULL
→
03keep matching rows
→
04result

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.

01PARSE

Understand SQL syntax

02SOURCE

Find tables and data

03FILTER

Remove rows that do not match

04COMBINE

Join or group data

05PROJECT

Produce requested columns

06SORT

Arrange the result

07RETURN

Send rows back

Visualize a real query→