Basic SQL Flashcards
Is it necessary to use capitalized commands when using SQL?
No, SQL will understand these commands if you type them in lowercase. Capitalizing commands is simply a convention that makes queries easier to read.
Does SQL care about spacing?
SQL treats one space, multiple spaces, or a line break as being the same thing.
If you want to have spaces in column names, you don’t need to refer to those columns in double quotes. True/False?
False. If you want to have spaces in column names, you need to always refer to those columns in double quotes.
If you’d like your results to look a bit more presentable, you can rename columns to include spaces. How can we do this? What’s something we need to pay attention to when naming the column? Code
SELECT west AS “West Region” # IT HAS TO BE IN DOUBLE QUOTATION
FROM tutorial.us_housing_units
The clauses always need to be in this order: SELECT, FROM, WHERE. True/False?
True
How can we use LIMIT command in SQL? Code
SELECT *
FROM tutorial.us_housing_units
LIMIT 100
Do the comparison operations in SQL work on non-numerical data?
All of the comparison operators work on non-numerical data as well.
What should we do to the non-numerical values of a column before using a comparison operator with them?
SQL uses single quotes to reference column values when using them with a comparison operator
How do “>” and “<” operate when used with a non-numerical value?
They filter based on alphabetical order
SQL doesn’t consider ‘Ja’ to be greater than ‘J’. True/False?
False, SQL considers ‘Ja’ to be greater than ‘J’ because it has an extra letter.
Write a query that only shows rows for which the month_name starts with the letter “N” or an earlier letter in the alphabet.
SELECT *
FROM tutorial.us_housing_units
WHERE month_name < ‘o’ #MUST BE SINGLE QUOTATION
How do arithmetic operators work in SQL? Row-wise or column-wise?
Column-wise (across columns)
What should we do if we need to do arithmetic across rows? (values in a column)
you’ll need to use aggregate functions
You can chain arithmetic functions, including both column names and actual numbers. True/False?
True.
SELECT year,
month,
west,
south,
west + south - 4 * year AS nonsense_column
FROM tutorial.us_housing_units
What are the columns that contain the arithmetic functions called?
Derived columns
As in Excel, you can use parentheses in SQL to manage the order of operations. True/False?
True
What is the use of logical operators in SQL?
Logical operators allow you to use multiple comparison operators in one query.
What are the logical operators of SQL? What do they do?
LIKE allows you to match similar values, instead of exact values.
IN allows you to specify a list of values you’d like to include.
BETWEEN allows you to select only rows within a certain range.
IS NULL allows you to select rows that contain no data in a given column.
AND allows you to select only rows that satisfy two conditions.
OR allows you to select rows that satisfy either of two conditions.
NOT allows you to select rows that do not match a certain condition.
What does this code do?
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” LIKE ‘Snoop%’
the results from the Billboard Music Charts dataset will include rows for which “group” starts with “Snoop” and is followed by any number and selection of characters.
Why in this code, group is in double quotation?
SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” LIKE ‘Snoop%’
“group” appears in quotations above because GROUP/group is actually the name of a function in SQL. The double quotes (as opposed to single: ‘) are a way of indicating that you are referring to the column name “group”, not the SQL function. In general, putting double quotes around a word or phrase will indicate that you are referring to that column name.