Understand what SQL queries do with plain English explanations. Perfect for learning SQL or understanding complex queries.
Understanding SQL queries is essential for working with databases effectively. This tool breaks down complex queries into understandable explanations, helping you learn SQL or understand code written by others.
SELECT column1, column2
FROM table_name
WHERE condition;
Explanation: Retrieves specific columns from a table where rows meet the specified condition.
SELECT users.name, orders.total
FROM users
JOIN orders ON users.id = orders.user_id;
Explanation: Combines data from users and orders tables where the user ID matches, showing each user with their order totals.
SELECT category, COUNT(*) as count, AVG(price) as avg_price
FROM products
GROUP BY category
HAVING COUNT(*) > 10;
Explanation: Groups products by category, counts items in each category, calculates average price, and only shows categories with more than 10 products.
| Keyword | Purpose | Example |
|---|---|---|
| SELECT | Choose columns to retrieve | SELECT name, email |
| FROM | Specify source table | FROM users |
| WHERE | Filter rows | WHERE age > 18 |
| JOIN | Combine tables | JOIN orders ON users.id = orders.user_id |
| GROUP BY | Group rows for aggregation | GROUP BY category |
| HAVING | Filter groups | HAVING COUNT(*) > 5 |
| ORDER BY | Sort results | ORDER BY created_at DESC |
| LIMIT | Restrict result count | LIMIT 10 |
SQL queries are written in one order but executed in a different order:
SELECT
c.name as customer_name,
COUNT(o.id) as total_orders,
SUM(o.total) as total_spent,
AVG(o.total) as avg_order_value
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE c.country = 'USA'
AND c.created_at >= '2024-01-01'
GROUP BY c.id, c.name
HAVING COUNT(o.id) > 3
ORDER BY total_spent DESC
LIMIT 20;
Plain English: Show me the top 20 US customers (by spending) who joined in 2024 or later and have placed more than 3 orders. For each customer, show their name, number of orders, total amount spent, and average order value, sorted by highest spenders first.