CompTIA DataSys+ (DS0-001)Database FundamentalsEasy
A developer needs to retrieve the names of all customers whose `CustomerID` is either 'CUST001', 'CUST005', or 'CUST010' from the `Customers` table. Which SQL DML clause is the MOST appropriate for specifying these multiple discrete conditions in the `WHERE` clause?
- ABETWEEN
- BAND
- CLIKE
- DIN
Show answer & explanationAnswer & explanation
Correct answer: D. IN
The `IN` clause is used to specify multiple values in a `WHERE` clause, allowing you to filter for records where a column's value matches any value in a provided list. This is perfect for selecting specific discrete `CustomerID`s.
Why the other options are wrong
- A. `BETWEEN` is used for specifying a range of values, not a set of discrete values.
- B. `AND` is used to combine multiple conditions, but for multiple discrete values of the *same* column, `IN` is more concise and efficient than multiple `OR` conditions.
- C. `LIKE` is used for pattern matching with wildcards, not for matching a list of exact values.
SQL IN Clause
A logical operator in SQL that allows you to specify multiple values in a WHERE clause. It checks if a column's value matches any value in a list of specified values.
- Used in the WHERE clause.
- Efficient for matching discrete values.
- Can be used with a list of values or a subquery.
- Equivalent to multiple OR conditions but more concise.
Memory trick: WHERE conditions use IN for lists, LIKE for patterns, BETWEEN for ranges!