Skip to content

Beginner · Databases and SQL basics · topic 17 of 54 · 25 min

SELECT and WHERE: finding rows

Explanation

SELECT picks which columns you want and WHERE picks which rows. With =, >, <, AND, OR, IN, BETWEEN, LIKE and IS NULL you can ask almost any question of a table, like "students from Pune with marks above 70".

Choose the columns

SELECT is followed by the columns you want, separated by commas, then FROM and the table name. SELECT * means all columns, but in real apps it is better to name only the columns you need: less data travels and the code is clearer.

sql
SELECT name, marks FROM students;
Only two columns, for every student.

Choose the rows with WHERE

WHERE is followed by a condition: a test that is true or false for each row. Only the rows where it is true come back.

Think of it like this: WHERE is like the filter in a shopping app. The shop has thousands of shoes, but you tick "size 8", "under 2000 rupees" and "black". The app shows only the shoes that pass every tick. The shoes are still in the shop; the filter only changes what you see.
OperatorMeaningExample
= and <>equal, not equalcity = 'Pune', course <> 'BCom'
> < >= <=bigger, smallermarks >= 60
ANDboth must be truecity = 'Pune' AND marks > 70
ORat least one is truecity = 'Pune' OR city = 'Delhi'
IN (...)equal to any in the listcourse IN ('BCA', 'BSc')
BETWEEN a AND bfrom a to b, both includedmarks BETWEEN 60 AND 80
LIKEmatches a text patternname LIKE 'A%'
IS NULLthe value is missingphone IS NULL
sql
SELECT name, marks
FROM students
WHERE city = 'Pune' AND marks > 70;
Students from Pune who scored more than 70.

Patterns with LIKE

In LIKE, % means "any number of any characters" and _ means "exactly one character". 'A%' matches names that start with A, '%Kumar' matches names that end with Kumar, and '%an%' matches names that contain an anywhere.

sql
SELECT name FROM students WHERE name LIKE '%Kumar';
Every student whose name ends with Kumar.

Missing values: NULL

NULL means "no value": unknown or not filled in. It is not zero and not an empty text. Because nothing is equal to an unknown value, you must test it with IS NULL or IS NOT NULL.

Careful: WHERE phone = NULL never finds anything, not even rows where phone is missing. Always write WHERE phone IS NULL. Also, mixing AND with OR without brackets is a classic bug: write WHERE city = 'Pune' AND (course = 'BCA' OR course = 'BSc').
Tip: IN is a short way to write many ORs: course IN ('BCA', 'BSc', 'BCom') is the same as three conditions joined with OR.

Watch the lesson

SELECT and WHERE: finding rows · 1/7

SELECT and WHERE

Today we learn to ask questions of a table: which columns, and which rows.

Prefer a video by a teacher? Find free videos on “sql select where clause tutorial for beginners” on YouTube ↗

In real life: Scholarship list at a college

A college gives a scholarship to BCA and BSc students from Maharashtra who scored between 60 and 80 and have a phone number on record, so the office can call them. One query does it: SELECT name, phone FROM students WHERE course IN ('BCA', 'BSc') AND marks BETWEEN 60 AND 80 AND phone IS NOT NULL. The clerk gets the call list in a second instead of going through the register page by page.

Practice (0/3 done)

Task 1 · Code · SQL

The students table has id, name, city, course, marks and phone. Show the name and marks of every student from Pune.

Task 2 · Code · SQL

Show the name of every student who studies BCA or BSc AND has marks from 60 to 80 (both included). Use IN and BETWEEN.

Task 3 · Code · SQL

The office wants to call every student, but some have no phone number saved (the value is NULL). Show the name of each student whose phone is missing.

Quick quiz

Question 1

1.What does SELECT name FROM students WHERE marks > 80 return?

Question 2

2.Does marks BETWEEN 60 AND 80 include a student with exactly 80?

Question 3

3.Which names match LIKE 'S%'?

Question 4

4.How do you find rows where email is missing?

Question 5

5.What is a shorter way to write city = 'Pune' OR city = 'Delhi' OR city = 'Goa'?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: What is a database?