Lesson · ⏱ 7 min read

🗄️ SQL basics: asking a database questions

EnglishHinglish

🏠 Real-life example

A library has a catalogue. You don't search every shelf. You ask, 'Show me all books by Premchand published after 1920, sorted by title.' SQL is the language for asking a database exactly that kind of question.

💡 In simple words

Most company data lives in relational databases: tables with rows and columns. SQL (Structured Query Language) reads that data. SELECT picks columns, FROM picks the table, WHERE filters rows, ORDER BY sorts, and LIMIT caps the number of rows. Aggregates like COUNT, SUM and AVG summarise data. GROUP BY gives you one summary per group, and HAVING filters those groups. Nearly every data, ML and AI-product role uses SQL every week.

🪜 Step by step

  1. SELECT the columns you want FROM a table: SELECT name, city FROM customers;
  2. Filter rows with WHERE: WHERE city = 'Pune' AND signup_date >= '2026-01-01'.
  3. Summarise with GROUP BY: SELECT city, COUNT(*) AS customers FROM customers GROUP BY city;
  4. Filter groups with HAVING (after grouping) and sort with ORDER BY ... DESC, then LIMIT 10 for a top 10.
  5. Practise on a free sample database (SQLite, or online SQL playgrounds) with real business questions.

🔑 Key terms

SELECT … FROM
Choose which columns to read, and from which table.
WHERE
Keep only rows that match a condition.
GROUP BY
Summarise rows per group, e.g. orders per city.

👀 See it in action

sql
SELECT city, COUNT(*) AS orders, AVG(amount) AS avg_bill
FROM orders
WHERE order_date >= '2026-01-01'
GROUP BY city
HAVING COUNT(*) > 100
ORDER BY orders DESC
LIMIT 5;

The top 5 busiest cities this year, and their average bill.

⚠️ Common mistakes to avoid

  • Using WHERE to filter aggregates: WHERE COUNT(*) > 5 fails. Use HAVING for conditions on groups.
  • Comparing with NULL using =. Use IS NULL or IS NOT NULL.
  • SELECT * on huge tables in production. Select only the columns you need; it's faster and cheaper.

✅ Quick check

Which clause keeps only orders above ₹500?

  1. WHERE amount > 500
  2. ORDER BY amount
  3. GROUP BY amount
Show answer

A. WHERE amount > 500: WHERE filters rows by a condition. ORDER BY only sorts them.

🙋 Questions learners ask

Which SQL should I learn: MySQL, PostgreSQL, SQL Server?

The basics are almost identical in all of them. PostgreSQL is a great default; you can switch easily later.

Why do AI roles need SQL?

Most company data lives in databases and warehouses. Training data, evaluation data and metrics usually start with a SQL query.

📌 Remember

SELECT what → FROM where → WHERE which rows → GROUP BY → ORDER BY.

Learn this as part of a full course

Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:

AI Foundations Path · Data Engineer (AI focus) · AI Product Manager

Continue in the course