Mikkel84
10/8/2018 - 7:24 AM

[where] #SQL #DB

[where] #SQL #DB

-- only 2010
SELECT * FROM station_data
WHERE year = 2010;

-- everything but 2010
WHERE year != 2010
-- or
WHERE year <> 2010


-- inclusive range
WHERE year BETWEEN 2005 and 2010

-- AND, OR, IN
WHERE year >= 2005 AND year <= 2010

-- to get quarters
WHERE MONTH IN (3.6.9.12)
WHERE MONTH NOT IN (3,6,9,12)
WHERE MONTH % 3 = 0

-- with text
WHERE report_code = '513A63'
WHERE report_code IN ('513A63', '513A64', '513A65')

-- query for every report code that does not have 6 characters
WHERE length(report_code) != 6

-- wildcards with LIKE (% = any no. of characters; _ = any single character)
WHERE report_code LIKE 'A%' -- all codes starting with an A
WHERE report_code LIKE 'B_C%' -- all codes with B at 1st / C at 3rd char followed by any char

INSTR
SUBSTR
REPLACE

-- Booleans (1=true, 0=false)
WHERE tornado = 1 AND hail = 1
WHERE tornado AND hail
WHERE tornado = 0 AND hail = 1
WHERE NOT tornado AND hail

WHERE (rain = 1 AND temp <= 32)
OR snow > 0


SELECT COUNT(*) AS record_count FROM station_data
WHERE tornado = 1