Skip to content

SQL

SQL (Structured Query Language) is the standard language for relational database management systems (RDBMS).

  • Common RDBMS: PostgreSQL, MySQL, SQLite, Oracle, Microsoft SQL Server.
  • Relational Model: Data is organized into tables (rows and columns). Tables are related to one another via unique keys.

II. Retrieving Data (The SELECT Statement)

Section titled “II. Retrieving Data (The SELECT Statement)”

The core of SQL is the query. It declares what you want, not how to get it.

SELECT * FROM mytable; -- Select all columns
SELECT col1, col2 FROM mytable; -- Select specific columns
SELECT DISTINCT col1 FROM mytable; -- Select unique values (removes duplicates)

The WHERE clause filters rows before they are grouped or aggregated.

Comparison Operators

OperatorDescriptionExample
=, !=, <>Equal, Not Equalage != 20
>, <, >=, <=Greater/Less thanprice >= 100
BETWEEN .. ANDInclusive rangeyear BETWEEN 1990 AND 2000
IN (...)Matches any value in a liststatus IN ('Active', 'Pending')
IS NULLChecks for missing valuesemail IS NULL

Text Pattern Matching (LIKE)

SymbolMeaningExample
%Wildcard (0 or more characters)'A%' (Starts with A)
_Wildcard (exactly 1 character)'A_' (A followed by one char)
ILIKECase-insensitive LIKE (Postgres specific)name ILIKE 'john'
SELECT col1, col2 FROM mytable
ORDER BY col1 ASC, col2 DESC -- Sort direction
LIMIT 10 OFFSET 5; -- Skip first 5, take next 10

Normalization splits data into multiple tables to reduce redundancy. Joins allow us to reconstruct that data.

Join TypeDescription
INNER JOINReturns rows only when there is a match in both tables.
LEFT JOINReturns all rows from the left table, and matching rows from the right. (Fills unmatched with NULL).
RIGHT JOINReturns all rows from the right table, and matching rows from the left.
FULL JOINReturns rows when there is a match in one of the tables. (All rows from both).

Syntax:

SELECT t1.col, t2.col
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t1.id = t2.t1_id; -- The Join Condition

Aggregation collapses many rows into a single summary row.

FunctionDescription
COUNT(*)Count total rows.
COUNT(col)Count non-NULL rows in a column.
SUM(col)Total sum of numeric column.
AVG(col)Mean value.
MIN(col) / MAX(col)Smallest / Largest value.

Used to group rows that have the same values into summary rows.

SELECT department, COUNT(*) as emp_count
FROM employees
GROUP BY department;

Critical Distinction:

  • WHERE filters rows before grouping.
  • HAVING filters groups after aggregation.
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000; -- Only show departments with high avg salary

Combines the results of two separate queries.

  • UNION: Combines unique rows.
  • UNION ALL: Combines all rows (including duplicates) - Faster than UNION.
  • INTERSECT: Rows common to both queries.
  • EXCEPT (or MINUS): Rows in the first query but not the second.

A way to create a temporary result set for use within the main query. It makes code more readable than nested subqueries.

WITH regional_sales AS (
SELECT region, SUM(amount) as total_sales
FROM orders
GROUP BY region
)
SELECT * FROM regional_sales WHERE total_sales > 100000;

  • INSERT: INSERT INTO table (c1, c2) VALUES (v1, v2);
  • UPDATE: UPDATE table SET c1 = val WHERE condition;
  • DELETE: DELETE FROM table WHERE condition;
  • CREATE TABLE:
    CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(50) UNIQUE NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    );
  • ALTER TABLE: ALTER TABLE users ADD COLUMN email TEXT;
  • DROP TABLE: DROP TABLE IF EXISTS users;