← Back
LESSON 2
SQL (Core) - Queries with constraints (Pt. 1)
Prereq: None | Progress: 0%
Course Syllabus
Lesson 0 📖 What is SQL? Lesson 1 📖 SELECT queries 101 Lesson 2 📖 Queries with constraints (Pt. 1) Lesson 3 📖 Queries with constraints (Pt. 2) Lesson 4 📖 Filtering & sorting results Review 1 📖 Simple SELECT Queries Lesson 6 📖 Multi-table queries with JOINs Lesson 7 📖 OUTER JOINs Lesson 8 📖 A short note on NULLs Lesson 9 📖 Queries with expressions Lesson 10 📖 Queries with aggregates (Pt. 1) Lesson 11 📖 Queries with aggregates (Pt. 2) Lesson 12 📖 Order of execution of a Query Lesson 13 📖 Inserting rows Lesson 14 📖 Updating rows Lesson 15 📖 Deleting rows Lesson 16 📖 Creating tables Lesson 17 📖 Altering tables Lesson 18 📖 Dropping tables Lesson 19 📖 Subqueries & Nested Queries Lesson 20 📖 Set Operations Summary 🎓 Course Completion
Queries with constraints (Pt. 1)
When working with databases containing millions of rows, returning the entire table is inefficient. In order to filter results down to specific target records, we use the WHERE clause.
Select Query with Constraints Syntax
SELECT column, another_column FROM mytable WHERE condition AND/OR another_condition;
The WHERE clause is evaluated for each row in the table. If the condition resolves to TRUE, the row is included in the output; if FALSE, it is discarded.
| Operator | Condition | SQL Example |
|---|---|---|
| =, !=, <, <=, >, >= | Standard numerical comparison | year >= 2000 |
| BETWEEN ... AND ... | Number is within inclusive range | year BETWEEN 2000 AND 2010 |
| NOT BETWEEN ... AND ... | Number is outside inclusive range | year NOT BETWEEN 2000 AND 2010 |
| IN (...) | Number matches any item in a discrete list | id IN (1, 3, 5) |
| NOT IN (...) | Number matches no items in a discrete list | id NOT IN (2, 4, 6) |
Combining Logical Operators (AND / OR / NOT)
You can combine multiple criteria using AND (both conditions must be true) and OR (at least one condition must be true). Always use parentheses (...) when mixing AND and OR to explicitly dictate operator precedence.
🗄️ Schema Explorer SELECTABLE
› movies
|
🎯 Exercises Checklist:
☐ 1. Find the movie with a row id of 6
☐ 2. Find the movies released in the years between 2000 and 2010
☐ 3. Find the movies not released in the years between 2000 and 2010
☐ 4. Find the first 5 Pixar movies and their release year
⚡ Practice Console
💡 Hint:
Loading hint...
🔑 Solution SQL:
SELECT * FROM movies; Data Output
No results. Run query to fetch data.