w3d1 Flashcards
What is the syntax to check if an element is within a collection (in a WHERE clause)?
name IN (‘Denmark’, ‘Finland’, ‘Norway’, ‘Sweden’)
How can we retrieve a country that starts with the letter ‘G’
name LIKE ‘G%’;
Explain regex syntax for finding words that contain letters.
Use ‘LIKE’, then put a ‘%’ at the end of the string you are searching for.
In which years was the Physics prize awarded, but no Chemistry prize?
SELECT DISTINCT yr FROM nobels WHERE (subject = 'Physics' AND yr NOT IN ( SELECT yr FROM nobels WHERE subject = 'Chemistry' ))
T/F: values from an inner SELECT are inaccessible from an outer SELECT.
F; they are.
What is the order of evaluation in the SELECT-FROM-WHERE triad?
FROM is evaluated first, then WHERE, then SELECT.
Find each country that belongs to a continent where all populations are
less than 25,000,000. Show name, continent and population.
SELECT c1.name, c1.continent, c1.population FROM countries c1 WHERE c1.continent NOT IN ( SELECT c2.continent FROM countries c2 WHERE c2.population >= 25000000 );
How do we get rid of repetition in SELECT clauses?
Use SELECT DISTINCT
What is up with the GROUP BY clause?
Tells how to combine elements:
SELECT continent, COUNT(*) FROM countries WHERE population >= 10000000 GROUP BY continent;
Explain JOIN
You JOIN tables ON a value = another (logically correlated) value. The resulting table now has access to EVERYTHING from the previous tables.
What does the COALESCE keyword do?
Retrieves the first value from a list of values that is not NULL.
Explain the CASE-WHEN-ELSE clauses. Where are they used?
They are used within a SELECT clause, and follow the following syntactical guidelines:
SELECT teachers.name, CASE WHEN teachers.dept_id IN (1, 2) THEN 'Sci' ELSE 'Art' END AS dept_name FROM teachers;
What does an unqualified JOIN represent?
INNER JOIN