Bird
Raised Fist0
SQLquery~10 mins

WHERE with comparison operators in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - WHERE with comparison operators
Start Query
↓
Read FROM table
↓
Apply WHERE condition
↓
Evaluate comparison operator
↓
Row matches?
↓
Include row
↓
Return filtered rows
↓
End Query
The query reads the table, applies the WHERE condition using comparison operators to each row, includes rows that match, excludes others, and returns the filtered result.
Execution Sample
SQL
SELECT * FROM employees WHERE salary > 50000;
This query selects all employees whose salary is greater than 50000.
Execution Table
StepRow IDsalaryCondition (salary > 50000)Include Row?
114800048000 > 50000 = FalseNo
225200052000 > 50000 = TrueYes
335000050000 > 50000 = FalseNo
447500075000 > 50000 = TrueYes
553000030000 > 50000 = FalseNo
666000060000 > 50000 = TrueYes
Exit--No more rows to check-
💡 All rows checked; query returns only rows where salary > 50000 is True.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5After Row 6Final
Current Row ID-123456-
Condition Result-FalseTrueFalseTrueFalseTrue-
Included Rows[][][2][2][2,4][2,4][2,4][2,4,6]
Key Moments - 2 Insights
Why is the row with salary 50000 not included even though it equals 50000?
Because the condition uses '>' (greater than), which excludes values equal to 50000. Only values strictly greater than 50000 pass, as shown in execution_table rows 3 and 4.
What happens if the condition is false for a row?
The row is excluded from the result. For example, row 1 with salary 48000 fails the condition and is not included, as seen in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which row is the first to be included in the result?
ARow 1
BRow 2
CRow 3
DRow 5
💡 Hint
Check the 'Include Row?' column in execution_table for the first 'Yes'.
At which step does the condition 'salary > 50000' become false for the first time?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Condition (salary > 50000)' column in execution_table for the first 'False'.
If the condition changed to 'salary >= 50000', which row would be included that was previously excluded?
ARow with salary 48000
BRow with salary 30000
CRow with salary 50000
DRow with salary 60000
💡 Hint
Check which row had salary exactly 50000 and was excluded before in execution_table.
Concept Snapshot
WHERE clause filters rows using comparison operators.
Syntax: WHERE column operator value
Common operators: =, >, <, >=, <=, <>
Only rows meeting the condition are returned.
Comparison is done row by row.
Full Transcript
This visual execution shows how the SQL WHERE clause works with comparison operators. The query reads each row from the employees table and checks if the salary is greater than 50000. For each row, the condition is evaluated: if true, the row is included in the result; if false, it is excluded. The execution table lists each row's salary, the condition result, and whether the row is included. The variable tracker shows how the current row and included rows change step by step. Key moments clarify why equal values are excluded with '>' and what happens when the condition is false. The quiz tests understanding by asking about specific steps and effects of changing the condition. This helps beginners see exactly how filtering works in SQL queries.

Practice

(1/5)
1. What does the WHERE clause do in an SQL query?
easy
A. Filters rows based on a condition
B. Sorts rows in ascending order
C. Joins two tables together
D. Creates a new table

Solution

  1. Step 1: Understand the purpose of WHERE

    The WHERE clause is used to select only those rows that meet a specific condition.
  2. Step 2: Compare with other SQL clauses

    Sorting is done by ORDER BY, joining by JOIN, and creating tables by CREATE TABLE, so these are not correct.
  3. Final Answer:

    Filters rows based on a condition -> Option A
  4. Quick Check:

    WHERE filters rows = A [OK]
Hint: WHERE filters rows; ORDER BY sorts rows [OK]
Common Mistakes:
  • Confusing WHERE with ORDER BY
  • Thinking WHERE creates or joins tables
  • Assuming WHERE changes table structure
2. Which of the following is the correct syntax to select rows where the column age is greater than 30?
easy
A. SELECT * FROM table WHERE age >= 30;
B. SELECT * FROM table WHERE age > 30;
C. SELECT * FROM table WHERE age >> 30;
D. SELECT * FROM table WHERE age = > 30;

Solution

  1. Step 1: Identify correct comparison operator

    The operator for 'greater than' is >, so age > 30 is correct.
  2. Step 2: Check syntax correctness

    The option with SELECT * FROM table WHERE age = > 30; has invalid operator '= >', the one with '>>' uses bitwise shift, and the one with '>=' means 'greater or equal to', not strictly greater.
  3. Final Answer:

    SELECT * FROM table WHERE age > 30; -> Option B
  4. Quick Check:

    Use > for greater than, not >= or >> [OK]
Hint: Use > for greater than, >= for greater or equal [OK]
Common Mistakes:
  • Using '= >' instead of '>'
  • Confusing '>' with '>>'
  • Using '>=' when only '>' is needed
3. Given the table Employees with columns id, name, and salary, what rows will this query return?
SELECT * FROM Employees WHERE salary <= 50000;
medium
A. All employees with salary greater than 50000
B. All employees with salary exactly 50000 only
C. All employees with salary less than or equal to 50000
D. No employees, syntax error

Solution

  1. Step 1: Understand the comparison operator used

    The operator '<=' means 'less than or equal to', so it selects salaries 50000 or less.
  2. Step 2: Interpret the query result

    The query returns all rows where salary is 50000 or below, not just exactly 50000 or above 50000.
  3. Final Answer:

    All employees with salary less than or equal to 50000 -> Option C
  4. Quick Check:

    salary <= 50000 means salary ≤ 50000 [OK]
Hint: <= means less than or equal to [OK]
Common Mistakes:
  • Thinking <= means only equal
  • Confusing <= with <
  • Assuming syntax error in query
4. Identify the error in this SQL query:
SELECT * FROM Products WHERE price <>;
medium
A. Wrong comparison operator used
B. Incorrect table name
C. Missing SELECT keyword
D. Missing value after comparison operator

Solution

  1. Step 1: Analyze the WHERE clause

    The operator '<>' means 'not equal', but it must be followed by a value to compare.
  2. Step 2: Identify syntax error

    Here, '<>' is not followed by any value, causing a syntax error.
  3. Final Answer:

    Missing value after comparison operator -> Option D
  4. Quick Check:

    Comparison operators need a value after them [OK]
Hint: Comparison operators must have a value after them [OK]
Common Mistakes:
  • Leaving out value after operator
  • Confusing <> with != (both valid but must have value)
  • Assuming table or SELECT keyword is missing
5. You want to select all orders from the Orders table where the order_date is after January 1, 2023, and the total_amount is at least 100. Which query correctly uses WHERE with comparison operators?
hard
A. SELECT * FROM Orders WHERE order_date > '2023-01-01' AND total_amount >= 100;
B. SELECT * FROM Orders WHERE order_date >= '2023-01-01' OR total_amount > 100;
C. SELECT * FROM Orders WHERE order_date < '2023-01-01' AND total_amount > 100;
D. SELECT * FROM Orders WHERE order_date = '2023-01-01' AND total_amount = 100;

Solution

  1. Step 1: Understand the date condition

    We want orders after January 1, 2023, so use 'order_date > '2023-01-01''.
  2. Step 2: Understand the amount condition

    We want total_amount at least 100, so use 'total_amount >= 100'. Both conditions must be true, so use AND.
  3. Step 3: Check each option

    SELECT * FROM Orders WHERE order_date > '2023-01-01' AND total_amount >= 100; matches both conditions correctly. SELECT * FROM Orders WHERE order_date >= '2023-01-01' OR total_amount > 100; uses OR and >= for date, which is incorrect. SELECT * FROM Orders WHERE order_date < '2023-01-01' AND total_amount > 100; uses < for date, wrong direction. SELECT * FROM Orders WHERE order_date = '2023-01-01' AND total_amount = 100; uses = which is too strict.
  4. Final Answer:

    SELECT * FROM Orders WHERE order_date > '2023-01-01' AND total_amount >= 100; -> Option A
  5. Quick Check:

    Use > for after date, >= for at least amount, AND to combine [OK]
Hint: Use AND to combine conditions, > for after date, >= for minimum amount [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using >= for date when strictly after is needed
  • Using = instead of comparison operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard