SQL Intro Flashcards

(31 cards)

1
Q

What are the BIG SIX statements and the correct order?

A
  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What command to select the database to work with?

A

use the “USE” command/statement

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

How do you add comments to the SQL script?

A

for single line comments use “–” or “#”
for multi line comment use “/*” at the beginning and end of comments

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

What are aliases and how do you use them?

A

an aliases allows you to assign a name to a custom column.

to use: count(customer_id) AS customer_count

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

What is the SELECT statement?

A

-this is the first statement and is required
-this is where you identify the columns to pull into your results
-separate columns with a comma

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

What is the FROM statement?

A

-this is the second statement and is required
-this is where you identify the table your query will select data from
-NOTE: you will need to use JOIN to use multiple tables

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

What is SELECT DISTINCT?

A

this is used, in place of SELECT, to return the distinct/unique values in those columns

**if you include mutiple columns, your result set will return all distinct combinations of values across those columns

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

What are the aggregate functions?

A

used with the GROUP BY function to provide group level summaries

Count(), Count Distinct(), MIN(), MAX(), AVG(), and SUM()

Note for Count() and Count Distinct() NULL values get skipped. Need to add Count(*) to count the empty/null values

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

What is the WHERE statement?

A

-this is an option statement that comes after FROM

-this statement specifies criteria for filtering the records of your result set

  • you can use any of the logical operators to filter the results: = (equal), <> (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), BETWEEN, LIKE, and IN()

-if using a non numerical value such as a date or name, need to wrap that date in single quotes

-example code: WHERE category = ‘Sci-Fi’

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

What are the logical conditions you can use in your WHERE statement?

A

Yes, you can use the following:

-WHERE & AND
-WHERE & OR
-WHERE & IN
-WHERE & LIKE

this goes after the FROM statement

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

WHERE & AND. What is it and example code?

A

use AND to return results that satisfy both criteria

WHERE amount = 0.99
AND payment_date > ‘2006-01-01’

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

WHERE & OR. What is it and example code?

A

use OR to return records which satisfy any criteria

WHERE amount = 0.99
OR amount = 0.5
OR amount = 0.25

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

WHERE & IN. What is it and example code?

A

use this instead of OR to simplify the code. this will save time and make the code easier to understand

OR Logic:

WHERE amount > 5
OR customer_id = 10
OR customer_id = 15
OR customer_id = 25

IN:

WHERE amount > 5
OR customer_id IN (10,15,25)

this results in the same results!

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

WHERE & LIKE. What is it and example code?

A

LIKE allows you to use pattern matching instead of exact matching

You can use two wildcards:

% - multiple characters (ie “%erry” will return blueberry, Burberry, Jerry, Terry, etc)

  • exactly one character (ie “_erry” will return Terry, Jerry, etc)

WHERE name LIKE ‘%erry’

IMPORTANT: with “_” you can specify the number of characters that should be a wildcard. for example, you can write “____ton” (4 underscores), and this will search all records for value/text ending in “ton” with 4 characters in front of it.

NOTE: Capitalization MATTERS!!

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

WHERE & NOT LIKE. What is it and example code?

A

NOT LIKE is used to filter out records where values match a pattern you provide. It uses the same wildcards and capitalization rules as LIKE

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

What is GROUP BY?

A

it is an optional statement that specifies how to group

allows us to perform segment analysis on the data

if you use an aggregate function in the SELECT statement, GROUP BY is required

example code:

GROUP BY
rating

17
Q

Can you use multiple GROUP BYs?

A

Yes, you can. This allows you to create groups and sub-groups in your result set.

Just like specifying multiple multiple row or column labels in a Pivot Table

example code:

GROUP BY
rating,
rental_duration,
replacement_cost

18
Q

What is HAVING and what is an example?

A

HAVING is an option statement, you can use with GROUP BY to limit your result set to groups which satisfy certain logical criteria

In other words, HAVING is a great way to limit your results to the most important groups for your business

this statement comes after GROUP BY but before ORDER BY

you use the same aggregate functions as WHERE

19
Q

What is ORDER BY and what is an example?

A

this is an optional statement, and is the last statement of the big six.

ORDER BY specifies the order in which your query results are displayed

if you want the list to be descending you need to write DESC, if not, the default setting is ascending

Example Code:

FROM payment
ORDER BY amount DESC

Note: you can use ORDER BY with multiple criteria. ie.

FROM payment
ORDER BY amount DESC, created_date DESC

**the list will be sorted on amount first, then created_date

20
Q

What is the LIMIT statement

A

this statement is written at the end. by default mysql will limit the list to a 1000 records but if you use the limit statement you can set the total records returned.

Example code:

LIMIT 5000

this will return 5000 records

21
Q

What is the CASE statement?

A

the CASE statement allow you to process a series of IF/THEN logical operators in a specific order.

it goes in the SELECT section

the logic is WHEN XX = YY, THEN ‘comment’

need to add END AS “columnName” at the end of the string

include an ELSE condition at the end as a catch all and write something like “Oops…check logic”

CASE
WHEN xx=xx, THEN ‘yy’
WHEN xx IN (1,3,5) THEN ‘yy’
ELSE ‘uh oh…check logic’
END AS ‘columnName’

22
Q

Using COUNT & CASE to pivot data (advanced)

A

excel’s ability to pivot to columns can be replicated in SQL

when using CASE pivoting, we use COUNT() and only count records that match certain criteria

need to use GROUP BY

Example code:

SELECT
film_id,
COUNT (CASE WHEN store_id = 1THEN
inventory_id ELSE NULL END) AS store_1_copies,
COUNT (CASE WHEN store_id = 2THEN
inventory_id ELSE NULL END) AS store_2_copies
COUNT (inventory_id) AS total_copies
GROUP BY
film_id
ORDER BY
film_id

23
Q

What are two key concepts to structured data??

A

Normalization - the process of structuring tables and columns in a relational database to minimize redundancy and preserve data integrity.

 -Benefits include: eliminating duplicate data, 
 reducing errors and anomalies
 -In practice normalization involves breaking out 
 data from a single merged table into multiple 
 merged tables

Cardinality - refers to the uniqueness in values in a column (or attribute) of a table, and is commonly used to describe how two tables relate (one to one, one to many, many to many
-Primary keys are unique and cannot repeat.
there is only one instance of each primary key
value in a column
-Foreign keys are non unique. They can repeat so
there can be many instances of each foreign key
value in a column
-We can create a one to many relationship by
connecting a foreign key in one table to a
primary key in another

24
Q

What are the most common JOIN types?

A
  1. INNER JOIN (will use this or LEFT JOIN the most)
  2. LEFT JOIN (will use this or INNER JOIN the most)
  3. RIGHT JOIN (rarely used)
  4. FULL OUTER JOIN
  5. UNION

*these are all used directly after the FROM statement

**remember, when using JOINs need to specify the table in front of the column (ie table.column)

***all require the use of ON to specify the link between the two tables

 table1.column_1=table2.column_1
25
What is an INNER JOIN?
INNER JOIN returns records from BOTH tables when there is a match, and excludes unmatched records Example code: From rental INNER JOIN customer ON rental.customer_id=customer.customer_id
26
What is LEFT JOIN?
Returns all data from LEFT table and any matched records from the RIGHT table If there is no match you will not lose data from your first table This is the most powerful JOIN. Any time you are working with a data set and you want to add data from an additional table but you don’t want to lose your previous records, you are going to use LEFT JOIN Example code: From rental LEFT JOIN customer ON rental.customer_id=customer.customer_id
27
What is RIGHT JOIN?
PRO TIP NEVER WRITE RIGHT JOIN. IT IS NEVER USED IN PRACTICE Returns all records from the RIGHT table, and any matched records from the LEFT table Example code: From rental RIGHT JOIN customer ON rental.customer_id=customer.customer_id
28
What is UNION JOIN?
-Returns all data from your first table and then returns all data from second table appended to the end (duplicates are removed). Just stacking two results on top of each other and removing duplicates. -If you don't want to remove duplications you need to use UNION ALL -The query requires TWO separate SELECT strings -Each SELECT statement must include the same number of columns (same column structure) and stacked columns must share the same compatible form If you don't want duplicates removed, use UNION ALL Example code: SELECT ID, Name, Department FROM Employees_US UNION SELECT ID, Name, Department FROM Employees_UK;
29
What is FULL OUTER JOIN?
What It Does: Combines rows from two tables side-by-side based on a common column (a key), such as ID. Key Point: If a row from one table doesn’t match a row in the other table, it still appears in the result, but the unmatched columns are filled with NULL. Use FULL OUTER JOIN: When you want to merge data from two tables based on a shared key, keeping all data even if there’s no match. Example: Merging customer lists from two systems to see which customers are in both and which are only in on Example Code: SELECT * FROM Employees_US FULL OUTER JOIN Employees_UK ON Employees_US.ID = Employees_UK.ID;
30
How do you bridge unrelated tables?
When you need to connect two tables which do not directly relate, look for a third table containing keys common to both; this can serve as a "bridge" to join your tables together. Need to use multiple INNER JOINs to connect the columns. see below for an example. Example code: SELECT film.film_id, film.title, category.name AS category_name FROM film INNER JOIN film_category ON film.film_id = film_category.film_id INNER JOIN category ON film_category.category_id = category.category_id;
31
What is multi condition JOINS?
It is used to make the JOIN happen for a specific criteria. In the example, below it was filter for horror. The second string is considered the multi condition JOIN. You can used multiple ANDs or ORs. Note the second query has better performance. Example Code. They produce the same results but the second one it better for performance. SELECT film.film_id, film.title, film.rating, category.name FROM film INNER JOIN film_category ON film.film_id = film_category.film_id INNER JOIN category ON film_category.category_id = category.category_id WHERE category.name = 'horror' ORDER BY film_id;