Code Examples Flashcards

1
Q

SELECT COUNT(*)
FROM table_name;

A

This counts all the rows in the Table.

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

SELECT SUM(downloads)
FROM table_name;

A

This adds all values in the downloads column of the Table.

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

SELECT MAX(downloads)
FROM table_name;

A

This returns the largest value in the downloads column of the Table.

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

SELECT MIN(downloads)
FROM table_name;

A

This returns the smallest value in the downloads column of the Table.

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

SELECT AVG(downloads)
FROM table_name;

A

This returns the average number of downloads column of the Table.

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

SELECT ROUND(price, 0)
FROM table_name;

A

This rounds the values in the price column Table to 0 decimal places.

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

SELECT year,
AVG(imdb_rating)
FROM movies
GROUP BY year
ORDER BY year;

A

This returns the the year and average imdb_rating values that are grouped and ordered by the year.

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

SELECT ROUND(imdb_rating),
COUNT(name)
FROM movies
GROUP BY 1
GROUP BY 1;

A

This rounds the imdb_rating values and counts all the columns in name from the Table movies and the values are grouped by the first column.

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

SELECT year,
genre,
COUNT(name)
FROM movies
GROUP BY 1, 2
HAVING COUNT(name) > 10;

A

This selects the year, genre, and counts the name column from the movies Table, Grouping them by column 1 and column 2 but only counting the name values that are greater than 10.

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

SELECT orders.order_id,
customers.customer_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id

A

INNER JOIN

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

SELECT *
FROM table1
LEFT JOIN table2
ON table1.c2 = table2.c2;

A

LEFT JOIN

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

SELECT shirts.shirts_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;

A

CROSS JOIN

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

SELECT *
FROM table1
UNION
SELECT *
FROM table2;

A

UNION

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

WITH previous_results AS (
SELECT …..
)
SELECT *
FROM previous_results
JOIN customers
ON _____ = ______;

A

WITH

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

What is a PRIMARY KEY?

A

Special columns

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

What are the requirements of a PRIMARY KEY?

A

None of the value can be NULL
Each value must be unique (can’t have duplicate values)
A Table can not have more than one PRIMARY KEY column

17
Q

What is a FOREIGN KEY?

A

A PRIMARY KEY that appears in a different Table.

18
Q

What is the difference between PRIMARY KEYs and FOREIGN KEYs?

A

PRIMARY KEYs are unique and FOREIGN KEYs have descriptive names.

19
Q

The most common types of JOIN do what with PRIMARY and FOREIGN KEYs?

A

The most common types of JOINs, will be joining a FOREIGN KEY from one Table with the PRIMARY KEY of another Table.

20
Q

SELECT column1, column2
FROM table_name;

SELECT *
FROM table_name;

A

SELECT query

21
Q

SELECT name AS ‘Titles’
FROM movies;

A

AS - renames column or Table

22
Q

SELECT DISTINCT tools
FROM inventory;

A

DISTINCT - removes duplicate values

23
Q

SELECT *
FROM movies
WHERE imdb_rating>8;

A

WHERE filters the result set to include rows where the condition is true

24
Q

SELECT *
FROM movies
WHERE name LIKE ‘Se_en’

A

LIKE is used with the WHERE clause to search for a specific pattern.

25
Q

SELECT *
FROM movies
WHERE name LIKE ‘A%’;

SELECT *
FROM movies
WHERE name LIKE ‘%A’;

SELECT *
FROM movies
WHERE name LIKE ‘%A%’;

A

The wildcard character, %, matches zero or missing letters in a patter.

Example one matches movies that begin with A
Example two matches movies that end with A
Example three matches movies that contain A in it

26
Q

SELECT name
FROM movies
WHERE imdb_rating IS NOT NULL;

A

Unknown values are indicated by NULL

IS NOT NULL
IS NULL

Will be used as comparison operators for WHERE clause and not = or !=

27
Q

SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999;

A

The result set will include movie with years from 1990 up to and including 1999

28
Q

SELECT *
FROM movies
WHERE name BETWEEN ‘A’ AND ‘J’;

A

The result set will include movies with the names that begin with the letter ‘A’ up to, but not including the letter ‘J’.

29
Q

SELECT *
FROM movies
WHERE year BETWEEN 1990 AND 1999
AND genre = ‘romance’;

A

With AND, both conditions must be true for the row to be included in the result

30
Q

SELECT *
FROM movies
WHERE year > 2014
OR genre = ‘action’;

A

With OR, if any of the conditions are true then the row is added to the result.

31
Q

SELECT *
FROM movies
WHERE imdb_rating > 8
ORDER BY year DESC;

A

ORDER BY sorts the result set by a particular column.
Always goes after WHERE clause if WHERE is present

32
Q

SELECT *
FROM movies
LIMIT 10;

A

LIMIT specifies the maximum number of rows the result will have
Always goes at the very end of the query

33
Q

SELECT name,
CASE
WHEN imdb_rating > 8 THEN ‘Fantastic’
WHEN imdb_rating > 6 THEN ‘Poorly Received’
ELSE ‘Avoid at All costs’
END AS ‘Review’
FROM movies;

A

CASE statement creates different outputs

-usually in the SELECT statement
- WHEN tests a condition
- THEN gives us the string
- CASE must end with END