SELECT Filters: DISTINCT, WHERE, IN, AND/OR & LIKE
Excel-style filtering done in SQL, plus new columns, case sensitivity and pattern matching with wildcards.
1. The Crux in 60 Seconds
DISTINCT removes repeats
SELECT DISTINCT ship_mode lists unique values; with several columns it lists unique combinations.
WHERE filters row by row
Operators = > < >= <= <>, BETWEEN and IN work on numbers, dates and strings. Quote strings and dates only.
AND narrows, OR widens
AND needs both conditions; OR needs either one. More ANDs mean fewer rows, more ORs mean more rows.
LIKE matches patterns
% = any number of characters, _ = exactly one, [ ] = a set or range. = only does exact matches.
2. Foundational Concepts & Comparisons
The instructor showed each idea in Excel first, then in SQL.
| In Excel you... | In SQL you write |
|---|---|
| Open a column's filter drop-down to see its values | SELECT DISTINCT ship_mode FROM orders |
| Tick one value (first class) | WHERE ship_mode = 'first class' |
| Tick two values on one column | WHERE ship_mode IN ('first class','same day') |
| Filter two different columns together | WHERE ... AND ... or ... OR ... |
| Type a formula such as profit / sales in a new column | SELECT *, profit / sales AS ratio |
Exact match vs pattern
=andINcompare exact values onlyLIKEcompares patterns, like a search box that keeps suggesting matches as you type- Several patterns need several
LIKEconditions joined byOR; IN cannot hold patterns
Recap of Day 2 (opening)
- ALTER: change type, add, drop columns; no data loss allowed and types must be compatible
- Constraints: primary key (can be several columns, written at the end), unique, check, not null
- INSERT with a column list; basic UPDATE and DELETE with a filter
- From here, most interview questions are SELECT questions
Opening doubt: default value vs a user-supplied value
One INSERT adds one row. A row that should use the default omits that column from the column list; a row with a given value (say credit card) is a separate INSERT. In a real app the back-end program decides: if the UI sends no payment method, use the default; otherwise pass what came.