SQL - MySQL

Module 01 - Database Theory
1. Introduction to Databases2. DBMS Theory Concepts3. Types of Keys4. Database Relationships5. DBMS Interview Questions
Module 02 - CRUD Operations
1. Create - INSERT2. Read - SELECT3. Update - UPDATE4. Delete - DELETE5. Alter - ALTER TABLE
Module 03 - Querying
1. Joins2. Filtering and sorting3. Practice - Filtering and Sort...4. Aggregate functions5. Practice - Aggregate Function...
Module 04 - Data Integrity
1. Constraints in Depth2. Transactions
MySQL Playground
ProfileProfile
Akkal DhamiFull Stack Developer

Building modern web experiences with a focus on performance, scalability, and clean architecture.

© 2026 | Akkal Dhami | All rights reserved

Built with
byAkkal Dhami

Navigation

  • Projects
  • Dev Setup
  • Playbook
  • Templates
  • Networking
  • SQL - MySQL
  • SQL Playground
  • System Design
  • DSA
AKKAL DHAMIAKKAL DHAMIAKKAL DHAMI

Querying

1. Joins

A JOIN combines rows from two (or more) tables based on a related column — usually a foreign key pointing to a primary key.

Instead of storing everything in one giant table, related data lives in separate tables, and joins stitch it back together when you need it.

  • INNER JOIN: Return only rows where both tables have matching data.
  • LEFT JOIN: Return all rows from the left table, plus matching rows from the right table. If no match, the right table's columns come back as NULL.
  • RIGHT JOIN: Return all rows from the right table, plus matching rows from the left table. If no match, the left table's columns come back as NULL.
  • CROSS JOIN: Return the Cartesian product of the two tables. Not supported in all SQL databases.
  • SELF JOIN: Return rows from a table that match rows from the same table.
example.sql
-- Inner Join
SELECT customers.name, orders.product
FROM customers
INNER JOIN orders
ON customers.id = orders.customer_id;
 
-- Left Join
SELECT customers.name, orders.product, orders.price
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;
 
-- Right Join
SELECT customers.name, orders.product, orders.price
FROM customers
RIGHT JOIN orders
ON customers.id = orders.customer_id;
 
-- Cross Join
SELECT customers.name, orders.product, orders.price
FROM customers
CROSS JOIN orders;
 
-- Self Join
SELECT e.Name AS Employee, m.Name AS Manager
FROM Employees e
INNER JOIN Employees m
ON e.ManagerID = m.EmpID;

2. Filtering and Sorting

ConceptKey takeawayExample
ANDBoth conditions must be trueWHERE a AND b
OREither condition can be trueWHERE a OR b
Operator precedenceAND evaluates before OR — always add parentheses when mixing themWHERE (a OR b) AND c
NOTInverts a conditionWHERE NOT genre = 'Sci-Fi'
BETWEENInclusive range checkWHERE price BETWEEN 10 AND 15
INMatches against a list — cleaner than chained ORWHERE author IN ('A', 'B')
NULL handling= NULL never matches — use IS NULL / IS NOT NULLWHERE genre IS NULL
ORDER BY (multiple columns)Second column only breaks ties in the firstORDER BY genre ASC, price ASC
ORDER BY (expression)Can sort by a calculated value, not just a raw columnORDER BY (2026 - published_year) DESC

One-line recap

WHERE filters which rows come back, AND/OR combine conditions (parenthesize when mixed), IN/BETWEEN simplify common patterns, NULL needs IS NULL not =, and ORDER BY controls result order — by column, multiple columns, or an expression.


3. Practice - Filtering & Sorting

1. Find all books priced under $12.

2. Find all books that are either Sci-Fi or Dystopian.

3. Find all books that are Sci-Fi and currently in stock.

4. Find all books published between 1940 and 1960 (inclusive).

5. Find all books written by either 'George Orwell' or 'Isaac Asimov' — write it using IN.

6. Find all books that are not in the Sci-Fi genre.

7. Write a query for: "books by George Orwell, OR any Dystopian book published after 1940."

8. Sort all books by price, most expensive first.

9. Sort all books by genre (A-Z), and within each genre, by price (cheapest first).

10. Find the 2 cheapest books that are currently in stock.

11. Find all books where author is missing. This table has no NULL authors right now — write the query as if it did, and explain why WHERE author = NULL wouldn't work.

12. Return the title and price of all out-of-stock books, sorted by how long ago they were published (oldest first), showing only the single oldest one.


4. Aggregate Functions

1. The core functions

example.sql
SELECT COUNT(*) FROM books;          -- how many rows: 5
SELECT SUM(price) FROM books;        -- total of all prices
SELECT AVG(price) FROM books;        -- average price
SELECT MIN(price) FROM books;        -- cheapest price
SELECT MAX(price) FROM books;        -- most expensive price

2. GROUP BY

example.sql
SELECT genre, AVG(price) AS avg_price
FROM books
GROUP BY genre;

+-----------+-----------+ | genre | avg_price | +-----------+-----------+ | Sci-Fi | 13.750000 | | Dystopian | 10.120000 | | Satire | 7.500000 | +-----------+-----------+

3. HAVING — filtering after grouping

example.sql
SELECT genre, COUNT(*) AS book_count
FROM books
GROUP BY genre
HAVING COUNT(*) > 1;

+-----------+------------+ | genre | book_count | +-----------+------------+ | Sci-Fi | 2 | | Dystopian | 2 | +-----------+------------+

4. Combining WHERE and HAVING

example.sql
SELECT genre, AVG(price) AS avg_price
FROM books
WHERE in_stock = TRUE
GROUP BY genre
HAVING AVG(price) > 10;

+-----------+-----------+ | genre | avg_price | +-----------+-----------+ | Sci-Fi | 15.500000 | | Dystopian | 10.120000 | +-----------+-----------+


5. Practice - Aggregate Functions

Aggregate Functions Playground

1. Count the total number of books in the table.

2. Find the total value of all books combined (sum of price).

3. Find the average price of all books.

4. Find the cheapest book's price.

5. Find the most expensive book's price.

6. Count how many books are currently in stock.

7. Find the number of books per genre.

8. Find the average price per author.

9. Find the total price of books per genre.

10. Find the earliest (MIN) and latest (MAX) published_year per author.

11. Find only the genres that have more than 2 books.

12. Find only the authors whose average book price is above $12.

13. Among books that are in stock, find the number of books per genre.

14. Find genres where the total combined price exceeds $20.

15. Explain why this query fails, and rewrite it correctly: SELECT genre, price, COUNT(*) FROM books GROUP BY genre;

16. Explain why this query fails, and rewrite it correctly: SELECT author, COUNT() FROM books WHERE COUNT() > 1 GROUP BY author;

17. Find each genre's average price, but only include genres where the average is above $10 and the genre has at least 2 books.

18. Find the author with the highest total combined value of books, sorted highest first, top 1 only.