⚡0 XP🔥0

SELECT and WHERE - Filtering Rows

Lesson 2 of 10 ⏱ 4 min read ✎ 4 exercises ❓ 5-question quiz ▶ Runs SQL

Choose columns, rename them with aliases, compute expressions, remove duplicates with DISTINCT, and filter rows with WHERE, AND/OR/NOT, BETWEEN, IN, LIKE and IS NULL.

Choosing columns#

List the columns you want, separated by commas, in the order you want them:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Tip

Prefer naming columns over SELECT * in real applications - the query is clearer, faster, and does not break when someone adds a column to the table.

Expressions and aliases#

You can compute new values in SELECT. AS gives a column a readable name (an alias):

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

|| joins text in SQLite and PostgreSQL. MySQL uses CONCAT(name, ' (', city, ')').

DISTINCT - remove duplicates#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

WHERE - filter rows#

WHERE keeps only rows for which the condition is true:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

Comparison operators: =, <> or != (not equal), >, <, >=, <=.

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
हिंदी में समझें

WHERE एक छलनी (filter) की तरह है - सिर्फ़ वही rows दिखती हैं जिनके लिए शर्त सच हो। Text values हमेशा single quotes में लिखें: 'Indore'। "बराबर नहीं" के लिए <> या != लिखते हैं।

Combining conditions: AND, OR, NOT#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Common Mistake

AND is evaluated before OR. Without the brackets, city = 'Bhopal' OR city = 'Ujjain' AND year = 1 would mean "everyone from Bhopal, plus first-years from Ujjain". Use brackets whenever you mix them.

BETWEEN and IN#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
  • BETWEEN a AND b includes both ends.
  • IN (...) is a short form of several OR conditions.

LIKE - pattern matching#

Pattern Matches
'A%' starts with A
'%a' ends with a
'%sh%' contains "sh"
'_a%' second letter is a
'____' exactly 4 characters
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Note

In SQLite and MySQL, LIKE ignores upper/lower case for English letters by default. In PostgreSQL it is case-sensitive; use ILIKE there for case-insensitive matching.

NULL - missing values#

NULL means "no value / unknown". It is not zero and not an empty string. Any comparison with NULL using = gives "unknown", so such rows are never returned. Use IS NULL and IS NOT NULL:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

The last query returns no rows even though three emails are missing - a very common mistake.

The order SQL runs a query#

You write SELECT ... FROM ... WHERE ..., but the database evaluates it as:

  1. FROM - pick the table
  2. WHERE - filter rows
  3. SELECT - choose and compute columns

That is why a column alias defined in SELECT cannot be used in WHERE of the same query.

Summary#

  • SELECT col1, col2 FROM table picks columns; AS renames; expressions compute new columns.
  • DISTINCT removes duplicates.
  • WHERE filters with =, <>, >, AND, OR, NOT, BETWEEN, IN, LIKE.
  • Use IS NULL / IS NOT NULL for missing values - never = NULL.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Students from Indore

Show the name and cgpa of all students whose city is Indore.

Database: college (see the tables in this lesson)
2

High CGPA in the first two years

Show name, year and cgpa of students in year 1 or 2 whose CGPA is at least 8.5.

Database: college (see the tables in this lesson)
3

Missing emails

List the student_id and name of students who have no email recorded.

Database: college (see the tables in this lesson)
4

Names with "ya"

Show the name and city of students whose name contains the letters ya anywhere.

Database: college (see the tables in this lesson)

Quick Quiz

Q1.

Which clause filters rows?

Q2.

WHERE name LIKE 'A%' matches...

Q3.

How do you find rows where email has no value?

Q4.

cgpa BETWEEN 7 AND 8 includes...

Q5.

What does SELECT DISTINCT city FROM students return?