Learn
Guide
SQL Fundamentals
Query and manipulate relational databases with confidence.
Practice with the tool:
SQL Query Analyzer →
SELECT & FROM
Retrieve all columns from a table:
SELECT * FROM users;
Retrieve specific columns with an alias:
SELECT name, email AS contact FROM users;
WHERE — Filtering
Filter rows by condition:
SELECT * FROM users WHERE age > 18;
SELECT * FROM orders WHERE status = 'pending' AND total > 100;
Useful operators: =, !=, >, <, >=, <=, LIKE, IN, BETWEEN, IS NULL
SELECT * FROM products WHERE name LIKE 'Phone%';
SELECT * FROM users WHERE id IN (1, 2, 3);
SELECT * FROM logs WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31';
JOIN — Combining Tables
-- INNER JOIN: only rows with a match in both tables
SELECT u.name, o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id;
-- LEFT JOIN: all rows from the left table, NULL for non-matching right rows
SELECT u.name, o.total
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;
| Join Type | Returns |
|---|---|
INNER JOIN |
Only matching rows |
LEFT JOIN |
All left + matched right |
RIGHT JOIN |
All right + matched left |
FULL JOIN |
All rows from both |
GROUP BY & Aggregates
SELECT department, COUNT(*) AS total, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING COUNT(*) > 5
ORDER BY avg_salary DESC;
| Function | Description |
|---|---|
COUNT(*) |
Number of rows |
SUM(col) |
Total of a column |
AVG(col) |
Average value |
MAX(col) |
Highest value |
MIN(col) |
Lowest value |