Syntax Flashcards
SELECT
The SELECT statement is used to select data from a database.
SELECT column1, column2, …
FROM table_name;
Here, column1, column2, … are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:
How can you select all data in a table?
SELECT * FROM …
How can I return only unique values from a column?
SELECT DISTINCT column FROM table
or
SELECT DISTINCT(column) FROM table
What does COUNT do?
The COUNT function returns the number of input rows that match a specific condition of a query.
We can apply COUNT on a specific column or just pass COUNT(*)
Ex: SELECT COUNT(*) FROM table;
Is the following a valid query:
SELECT COUNT DISTINCT amount FROM payment
No, COUNT needs to be followed up by parentheses: SELECT COUNT(DISTINCT amount) FROM payment;
What does WHERE do?
The WHERE statement allows us to specify conditions on columns for the rows to be returned.
The WHERE clause appears immediately after the FROM clause of the SELECT statement.
Is the following query correct:
SELECT * FROM film
WHERE rental_rate > 4 AND WHERE length > 100
No, WHERE cannot be repeated.
The correct query would be:
SELECT * FROM film
WHERE rental_rate > 4 AND length > 100
Is the following query correct:
SELECT title FROM film
WHERE rental_rate > 4 AND replacement_cost >= 19.99
AND rating=’R’;
Yes, you can add as many logical statements as you want
What does ORDER BY do?
ORDER BY orders results.
How does ORDER BY order if left blank? (no following statement)
ORDER BY will order by ascending if left blank.
How can you use ORDER BY with multiple columns?
ORDER BY company,sales
How can I specify to ORDER BY ascending for the first statement and descending for the second statement?
ORDER BY store_id ASC,first_name DESC
What does the LIMIT command allow us to do?
The LIMIT command allows us to lmit the number of rows returned for a query.
Useful for not wanting to return every single row in table, but only view the top few rows to get an idea of the table layout.
Where do ORDER BY and LIMIT belong inside a query?
ORDER BY and LIMIT belong at the very end of a query.
What does the BETWEEN operator do?
The BETWEEN operator can b e used to match a value against a range of values:
value BETWEEN low AND high
The BETWEEN operator is the same as:
value >= low AND value <= high
value BETWEEN low AND high
Can also combine BETWEEN with the NOT logical operator:
value NOT BETWEEN low AND high
In what format does a date need to be to be used with the BETWEEN operator?
ISO 8601 standard format:
YYYY-MM-DD
example:
date BETWEEN ‘2007-01-01’ AND ‘2007-02-01’
What does the IN operator do?
The IN operator can be used as a replacement to query multiple values.
We can use the IN operator to create a condition that checks to see if a value in included in a list of multiple options.
Example:
SELECT color FROM table
WHERE color IN(‘red,’blue’);
Considering the row you want to query is of a numeric datatype, how would you use the IN operator in that case?
WHERE amount IN (0.99,1.98,1.99)
as opposed to putting the values in quotation marks