SQL Flashcards

1
Q

UNION

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is SQL case sensitive for objects you are calling?

A

YES! you need to write it exactly as it is in the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When using group by, does it matter the order you list objects?

A

Yes! to group by that object first it needs to be listed first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

if you want to sort SQL columns what argument do you use?

A

ORDER BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you filter for even ID numbers only?

A

WHERE ID % 2 = 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to order alphabetically in SQL

A

ORDER BY (variable name) – it defaults to alphabetical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What can you use to connect to separate select causes?

A

UNION

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In MySQL, what do you use to find the length of a variable?

A

length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an easy way to find the max and min values of a table without using max or min?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how would you find values that started with vowels?

A

WHERE (value) LIKE ‘a%’ OR ‘e%’ OR ‘I%’ etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

when you are using OR, AND, in a where clause can you just use commas or do you need to be specific

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the easiest way to sort for variables that end and start with vowels?

A

use AND to connect LIKE a% AND %a for the whole list of vowels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the easiest way to sort variables so they do not start with vowels?

A

use NOT LIKE (all the vowels)% connecting them with AND instead of OR

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

when doing a join, does the name of the table go first or second in the select cause

A

first! ex: city.name ‘; city is the table, name is the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

describe a Left join

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

describe an inner join

A

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)

16
Q

describe a right join

A

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)

17
Q

Select all records where the first letter of the City is an “a” or a “c” or an “s”.

A

WHERE City LIKE ‘[acs]%’

18
Q

Select all records where the first letter of the City starts with anything from an “a” to an “f”.

A

WHERE City LIKE ‘[a-f]%’;

19
Q

Select all records where the first letter of the City is NOT an “a” or a “c” or an “f”.

A

WHERE City LIKE ‘[!acf]%’

20
Q

what does SQL stand for?

A

Structured Query Language

21
Q

TO DO:
- review CTE and subqueries
- review joins
- insert, delete, database?

A
22
Q

Self Join

A

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

23
Q

Cross join

A

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

24
Q

when do you use INTERSECT

A

Requires all fields to match, you don’t need to state what the ON statement is because they all match

INTERSECT operator is used to retrieve only the rows that are common between two SELECT queries. It returns unique records found in both queries’ result sets, making it useful when you need to find overlapping data between two datasets.

25
Q

EXCEPT

A

It effectively performs a “set difference” operation, allowing you to find records that exist in one dataset but not in another.

26
Q

when you do need to use GROUP BY?

A

when you have an agg function and non-agg functions - all non agg need to be in group by

26
Q

How do you create an anti-join?

A

Use a NOT statement into a CTE
EX: WHERE country NOT IN
(SELECT country FROM states WHERE independent_year < 1800)

27
Q

When do you use a subquery?

A

When you need to do a smaller query within a larger query in order to filter, aggregate, or transform the data you need

  1. Can place in WHERE to build filter or aggregate from other table
  2. Can place in FROM to build virtual table to pull from

these seem to be the most common based on the examples

28
Q

what does CTE stand for? What is it?

A

A Common Table Expression ; it is a temporary or virtual table that is present only during the calculation. Overall it helps to improve readability of results

WITH cte_name AS ([insert CTE]
SELECT *
FROM cte_name

29
Q

in what order does SQL run code?

A

“Find Wise Great Hunters Secretly Ordering Lunch”

FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT

30
Q
A