SQL Flashcards
(33 cards)
UNION
combines columns between data sets that have:
1. the same number of columns
2. similar data sets
you have to put the columns in the same order in the SELECT column
Is SQL case sensitive for objects you are calling?
YES! you need to write it exactly as it is in the table
When using group by, does it matter the order you list objects?
Yes! to group by that object first it needs to be listed first
if you want to sort SQL columns what argument do you use?
ORDER BY
How would you filter for even ID numbers only?
WHERE ID % 2 = 0
How to order alphabetically in SQL
ORDER BY (variable name) – it defaults to alphabetical
What can you use to connect to separate select causes?
UNION
In MySQL, what do you use to find the length of a variable?
length()
What is an easy way to find the max and min values of a table without using max or min?
select the first value (order by the variable in question and limit 1) , then use UNION to connect the same items but ORDER BY in desc (to get the last value), again limit 1
how would you find values that started with vowels?
WHERE (value) LIKE ‘a%’ OR ‘e%’ OR ‘I%’ etc
when you are using OR, AND, in a where clause can you just use commas or do you need to be specific
BE SPECIFIC ex:
WHERE (value) LIKE ‘a%’ OR ‘e%’ OR ‘I%’ etc
the OR has to be between each thing even if you want OR for all of them
what is the easiest way to sort for variables that end and start with vowels?
use AND to connect LIKE a% AND %a for the whole list of vowels
what is the easiest way to sort variables so they do not start with vowels?
use NOT LIKE (all the vowels)% connecting them with AND instead of OR
when doing a join, does the name of the table go first or second in the select cause
first! ex: city.name ‘; city is the table, name is the variable
describe a Left join
if we are thinking of a venn diagram, a left join takes all the information from the left table (first table called) then the information that matches the left table from the right table (table 2)
describe an inner join
if we are thinking of a venn diagram, an inner join is only the information in the middle. it only pulls the information that matches from the left (first table) and right (second table)
describe a right join
if we are thinking of a venn diagram, a right join takes all the information from the right table (2nd table) then the information that matches the left table (2nd table)
Select all records where the first letter of the City is an “a” or a “c” or an “s”.
WHERE City LIKE ‘[acs]%’
Select all records where the first letter of the City starts with anything from an “a” to an “f”.
WHERE City LIKE ‘[a-f]%’;
Select all records where the first letter of the City is NOT an “a” or a “c” or an “f”.
WHERE City LIKE ‘[!acf]%’
what does SQL stand for?
Structured Query Language
TO DO:
- review CTE and subqueries
- review joins
- insert, delete, database?
Self Join
self join allows you to compare data in the sample table as if it is from a different table. This can be helpful when you need to evaluate hierarchical relationships, compare rows in the same data or find duplicate rows
to do this, you give the table 2 different aliases to compare to
Cross join
a cross join combines every row of the first table to every row of the second table resulting in all possible pairs of rows. this is helpful if you have a small data set and you need to see all combinations