Stop 04 · SQL & DatabasesTopics · 1/6

SQL Fundamentals

Part of the Data Science Roadmap

In short

Company data lives in databases, and SQL is how you ask for it. It's tested in nearly every data science interview, and you'll use it more often than any ML library.

SELECT, WHERE, ORDER BY and basic filtering — the core statement pattern for pulling exactly the data you need, and the most common way real datasets reach a data scientist in the first place.

The mascot librarian pulls a few books off a tall wooden bookshelf.

Think of it like this

Asking the librarian precisely

You don't say 'give me books'. You say 'from the science shelf, only books after 2015, sorted by title, just the first ten'. The librarian fetches exactly that. A SQL query is that precise request, and the database is the librarian.

Watch it happen

The order SQL actually runs a query

FROM
WHERE
SELECT
ORDER BY
LIMIT

1/5FROM customers: the database starts with the whole table.

The big ideas

What you need to know

Tables, rows, columns

Each table holds one kind of thing, like customers. Rows are records, columns are fields.

WHERE filters rows

Use =, >, IN, BETWEEN and LIKE. Combine with AND and OR, using brackets to be clear.

NULL means unknown

NULL isn't zero or empty text. Check it with IS NULL, never with = NULL.

Write order isn't run order

Knowing FROM and WHERE run before SELECT explains why you can't use a SELECT alias in WHERE.

See it in code

Walk through the code

sql
1SELECT name, city, total_spent2FROM customers3WHERE city IN ('pune', 'mumbai')4  AND total_spent > 50005ORDER BY total_spent DESC6LIMIT 10;

1/4Start from the customers table.

Read it like a sentence: from customers, where these conditions hold, show these columns, biggest first, top ten.

Save yourself the pain

Common mistakes

Oops

Writing WHERE phone = NULL to find missing phone numbers.

Do this instead

Use WHERE phone IS NULL. Any comparison with NULL using = is never true.

Oops

Using SELECT * on big tables out of habit.

Do this instead

Name the columns you need. It's faster, cheaper on warehouses and clearer to read.

Your mission

Ten queries in your browser

~30 min

Quick check

Why does WHERE total > 100 fail if total is an alias created in SELECT?

Go deeper

  1. 1Write 10 basic SELECT queries against a free sample database.
  2. 2Practice combining multiple WHERE conditions with AND/OR.
  3. 3Learn the clause order SQL requires (SELECT, FROM, WHERE, ORDER BY).

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge