FREE TUTORIALEVIKA ACADEMY
SQL Tutorial
Learn SQL from scratch — SELECT, WHERE, JOINs, aggregation, window functions, CTEs, and more. Every topic includes a live query and real output you can try yourself.
📦
Sample Dataset Used Throughout
All examples use these three tables — learn them once, use them everywhere.
products
| product_id | product_name | category | price |
|---|---|---|---|
| P01 | Wireless Mouse | Electronics | 899 |
| P02 | USB-C Hub | Electronics | 1499 |
| P03 | Notebook A5 | Stationery | 99 |
| P04 | Ballpoint Pens | Stationery | 49 |
| P05 | Desk Lamp | Furniture | 1199 |
| P06 | Ergonomic Chair | Furniture | 8999 |
| P07 | HDMI Cable | Electronics | 299 |
customers
| customer_id | customer_name | city |
|---|---|---|
| C01 | Amit Verma | Noida |
| C02 | Priya Sharma | Delhi |
| C03 | Rohan Mehta | Mumbai |
| C04 | Sneha Gupta | Noida |
| C05 | Arjun Singh | Bengaluru |
orders
| order_id | cust | prod | date | amount |
|---|---|---|---|---|
| 1001 | C01 | P01 | 2026-10-01 | 899 |
| 1002 | C02 | P02 | 2026-10-02 | 1499 |
| 1003 | C01 | P03 | 2026-10-03 | 495 |
| 1004 | C03 | P06 | 2026-10-04 | 8999 |
| 1005 | C04 | P05 | 2026-10-05 | 2398 |
| 1006 | C05 | P07 | 2026-10-06 | 299 |
| 1007 | C01 | P04 | 2026-10-08 | 490 |
SQL Basics
Introduction
→What is SQL, RDBMS, and how tables store data.
-- Our sample table SELECT * FROM products LIMIT 3;
OUTPUT ↓
SQL Syntax
→Statement structure, case rules, semicolons, comments.
-- Keywords are case-insensitive SELECT product_name -- column FROM products; -- table
OUTPUT ↓
SELECT
→Pick specific columns or all columns from a table.
SELECT product_name, price FROM products LIMIT 3;
OUTPUT ↓
SELECT DISTINCT
→Remove duplicates — return only unique values.
SELECT DISTINCT category FROM products;
OUTPUT ↓
WHERE
→Filter rows using conditions and comparison operators.
SELECT product_name, price FROM products WHERE price > 1000;
OUTPUT ↓
AND, OR, NOT
→Combine multiple conditions in a WHERE clause.
SELECT product_name, category, price FROM products WHERE category = 'Electronics' AND price < 1000;
OUTPUT ↓
ORDER BY
→Sort results ascending or descending.
SELECT product_name, price FROM products ORDER BY price DESC LIMIT 3;
OUTPUT ↓
INSERT INTO
→Add one or more new rows to a table.
INSERT INTO products
(product_name, category, price)
VALUES
('Standing Desk','Furniture',12999);OUTPUT ↓
NULL Values
→NULL means missing/unknown — not zero, not blank.
SELECT customer_name FROM customers WHERE email IS NULL;
OUTPUT ↓
UPDATE
→Change existing column values with SET … WHERE.
UPDATE products SET price = 999 WHERE product_id = 1; -- Always use WHERE!
OUTPUT ↓
DELETE
→Remove rows that match a condition.
DELETE FROM orders WHERE total_amount < 100;
OUTPUT ↓
SELECT TOP / LIMIT
→Cap the number of rows returned — foundation of pagination.
SELECT product_name, price FROM products ORDER BY price DESC LIMIT 3;
OUTPUT ↓
SQL Aggregation
MIN & MAX
→Find the smallest and largest values in a column.
SELECT MIN(price) AS cheapest, MAX(price) AS priciest FROM products;
OUTPUT ↓
COUNT, SUM, AVG
→Count rows, total a column, calculate averages.
SELECT COUNT(*) AS orders, SUM(total_amount) AS revenue, ROUND(AVG(total_amount),0) AS avg_order FROM orders;
OUTPUT ↓
GROUP BY
→Split rows into groups and aggregate each group separately.
SELECT category, COUNT(*) AS products FROM products GROUP BY category;
OUTPUT ↓
HAVING
→Filter groups after aggregation — WHERE for aggregates.
SELECT category, COUNT(*) AS cnt FROM products GROUP BY category HAVING COUNT(*) > 2;
OUTPUT ↓
SQL Filtering & Conditionals
LIKE & Wildcards
→Pattern matching using % (any chars) and _ (one char).
SELECT product_name FROM products WHERE product_name LIKE '%Cable%';
OUTPUT ↓
IN & BETWEEN
→Match a list of values or filter within a range.
SELECT product_name, price FROM products WHERE price BETWEEN 500 AND 1500;
OUTPUT ↓
Aliases (AS)
→Rename columns and tables to make queries readable.
SELECT product_name AS name, price AS cost FROM products AS p LIMIT 2;
OUTPUT ↓
CASE WHEN
→If-else logic inside a query — classify, label, sort.
SELECT product_name,
CASE
WHEN price < 200 THEN 'Budget'
WHEN price < 1000 THEN 'Mid'
ELSE 'Premium'
END AS tier
FROM products LIMIT 4;OUTPUT ↓
SQL Joins
Joins Overview
→Why joins exist and the four types: INNER, LEFT, RIGHT, FULL.
-- orders links to customers SELECT o.order_id, c.customer_name FROM orders AS o JOIN customers AS c ON o.customer_id = c.customer_id LIMIT 3;
OUTPUT ↓
INNER JOIN
→Return only rows with a match in both tables.
SELECT o.order_id, c.customer_name, p.product_name FROM orders o JOIN customers c ON o.customer_id=c.customer_id JOIN products p ON o.product_id=p.product_id LIMIT 3;
OUTPUT ↓
LEFT JOIN
→All rows from left table; NULL where no match on right.
SELECT c.customer_name, o.order_id FROM customers AS c LEFT JOIN orders AS o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;
OUTPUT ↓
RIGHT JOIN
→All rows from right table; NULL where no match on left.
SELECT p.product_name, o.order_id FROM orders AS o RIGHT JOIN products AS p ON o.product_id = p.product_id WHERE o.order_id IS NULL;
OUTPUT ↓
FULL OUTER JOIN & UNION
→Every row from both tables; NULLs fill the gaps.
-- MySQL workaround SELECT c.customer_name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id=o.customer_id UNION SELECT c.customer_name, o.order_id FROM customers c RIGHT JOIN orders o ON c.customer_id=o.customer_id;
OUTPUT ↓
SQL Advanced
Subqueries
→Nest a SELECT inside WHERE, FROM, or SELECT.
SELECT product_name, price FROM products WHERE price > ( SELECT AVG(price) FROM products );
OUTPUT ↓
CTEs (WITH)
→Named temporary result sets for readable multi-step queries.
WITH totals AS (
SELECT customer_id,
SUM(total_amount) AS spent
FROM orders GROUP BY customer_id
)
SELECT * FROM totals
ORDER BY spent DESC LIMIT 3;OUTPUT ↓
Window Functions
→OVER(), PARTITION BY — calculate across rows without collapsing.
SELECT product_name, category, price,
SUM(price) OVER
(PARTITION BY category)
AS cat_total
FROM products LIMIT 4;OUTPUT ↓
ROW_NUMBER & RANK
→Number and rank rows within partitions.
SELECT product_name, category, price,
RANK() OVER
(PARTITION BY category
ORDER BY price DESC) AS rnk
FROM products;OUTPUT ↓
LAG & LEAD
→Access values from the previous or next row.
SELECT order_id, total_amount,
LAG(total_amount)
OVER (ORDER BY order_date)
AS prev_order
FROM orders LIMIT 4;OUTPUT ↓
Want to go faster? Learn SQL with a mentor.
Live batches in Noida Sector 51. Hands-on projects, mock interviews, placement support.
Book Free Demo →