SQL2 Flashcards

1
Q

What will this produce

SELECT *
FROM tutorial.us_housing_units
WHERE month_name > ‘January’

A

Months Feb - December

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

What will this produce?

SELECT *
FROM tutorial.us_housing_units
WHERE month_name > ‘J’

A

All months with names longer then j including January

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

Write a query that only shows rows for which the month name is February.

A

SELECT *
FROM tutorial.us_housing_units
WHERE month_name = ‘February’

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

Write a query that only shows rows for which the month_name starts with the letter “N” or an earlier letter in the alphabet.

A

SELECT *
FROM tutorial.us_housing_units
WHERE month_name <= ‘O’

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

Write a query that adds the west and south housing as south_plus_west

A

Select year, month, west, south, west+south AS south_plus_west

FROM tutorial.us_housing_units

This only adds across rows, you need an aggregate function across multiple rows

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

select the average number of homes for south and west combined for each month and year. Order by month and year

A

SELECT year, month, avg(west+south) average_sw_housing
FROM tutorial.us_housing_units
GROUP BY 1,2
ORDER BY 1,2

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

Write a query that calculates the sum of all four regions in a separate column.

A

SELECT year, month, (west+south+northeast+midwest) as total_units
FROM tutorial.us_housing_units

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

get the south and west averages per ROW

A

SELECT year, month, west, south, (west + south)/2 AS south_west_avg

FROM tutorial.us_housing_units

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

Write a query that returns all rows for which more units were produced in the West region than in the Midwest and Northeast combined.

A

SELECT year, month, west, midwest, northeast
FROM tutorial.us_housing_units
WHERE west > (midwest + northeast)

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

Write a query that calculates the percentage of all houses completed in the United States represented by each region. Only return results from the year 2000 and later.

A

SELECT year, month,
(west/(west+south+northeast+midwest))*100 as west_percent,
(south/(west+south+northeast+midwest))*100 as south_percent,
(northeast/(west+south+northeast+midwest))*100 as northeast_percent,
(midwest/(west+south+northeast+midwest))*100 as midwest_percent
FROM tutorial.us_housing_units
WHERE year >=2000
ORDER BY 1,2

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

SQL

LIKE allows you to match similar values, instead of exact values.

A

allows you to match similar values, instead of exact values.

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

IN

A

allows you to specify a list of values you’d like to include.

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

Between

A

allows you to select only rows within a certain range.

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

IS NULL

A

allows you to select rows that contain no data in a given column.

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

AND

A

allows you to select only rows that satisfy two conditions.

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

OR

A

allows you to select rows that satisfy either of two conditions.

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

NOT

A

allows you to select rows that do not match a certain condition.

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

Select Rows where the “group” starts with Snoop

will this return a group that starts with snoop?

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE “group” LIKE ‘Snoop%’

Note: “group” appears in quotations above because 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.

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

Select groups that begin with Snoop while ignoring case.

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE “group” ILIKE ‘snoop%’

20
Q

select any groups that contain ‘dr ke’ the space indicates a missing letter

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE artist ILIKE ‘dr_ke’

21
Q

Write a query that returns all rows for which Ludacris was a member of the group. Remember that groups can contain multiple members

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” ILIKE ‘%ludacris%’

22
Q

Write a query that returns all rows for which the first artist listed in the group has a name that begins with “DJ”.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” LIKE ‘DJ%’

23
Q

select all rows where the year_rank was 1,2 or 3

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE year_rank IN (1, 2, 3)

24
Q

Select rows that have the following artists:

Taylor Swift

Usher

Ludacris

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE artist IN (‘Taylor Swift’, ‘Usher’, ‘Ludacris’)

25
Q

Write a query that shows all of the entries for Elvis and M.C. Hammer.

Hint: M.C. Hammer is actually on the list under multiple names, so you may need to first write a query to figure out exactly how M.C. Hammer is listed. You’re likely to face similar problems that require some exploration in many real-life scenarios.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” IN (‘Elvis Presley’,’M.C. Hammer’, ‘Hammer’)

26
Q

Find all rows where the year rank is between 5 and 10.

using BETWEEN and without BETWEEN

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE year_rank BETWEEN 5 AND 10

This also includes the years 5 and 10

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE year_rank >= 5 AND year_rank <= 10

27
Q

Write a query that shows the most popular 100 songs from January 1, 1985 through December 31, 1990.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “year” BETWEEN 1985 AND 1990
ORDER BY year_rank
LIMIT 100

28
Q

select all rows where the artist is empty

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE artist IS NULL

WHERE artist = NULL will not work—you can’t perform arithmetic on null values.

29
Q

Write a query that shows all of the rows for which song_name is null.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “song_name” IS NULL

30
Q

return all rows for top-10 recordings in 2012.

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year = 2012 AND year_rank <= 10

31
Q

return all top 10 songs from 2012 where the group contains features

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year = 2012 AND year_rank <= 10

AND “group” ILIKE ‘%feat%’

32
Q

Write a query that surfaces all rows for top-10 hits for which Ludacris is part of the Group.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “year_rank” >=10
AND “group” ILIKE ‘%ludacris%’

33
Q

Write a query that surfaces the top-ranked records in 1990, 2000, and 2010.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “year_rank” = 1
AND “year” IN (1990, 2000, 2010)

34
Q

Write a query that lists all songs from the 1960s with “love” in the title.

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year BETWEEN 1960 AND 1969

AND song_name ilike ‘%love%’

35
Q

find all rows for songs that were either ranked 5th or have the artist ‘Gotye’

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE year_rank = 5 OR artist = ‘Gotye’

36
Q

find all songs in 2013 where the group contained either Macklemore or timberlake

A

SELECT *

FROM tutorial.billboard_top_100_year_end

WHERE year = 2013

AND (“group” ILIKE ‘%macklemore%’ OR “group” ILIKE ‘%timberlake%’)

37
Q

Write a query that returns all rows for top-10 songs that featured either Katy Perry or Bon Jovi.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “year_rank” <=10
AND (“group” ILIKE ‘%katy perry%’ OR “group” ILIKE ‘%bon jovi%’)

38
Q

Write a query that returns all songs with titles that contain the word “California” in either the 1970s or 1990s.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “song_name” ILIKE ‘%california%’
AND ((“year” BETWEEN 1970 AND 1979) OR (“year” BETWEEN 1990 AND 1999))

39
Q

Write a query that lists all top-100 recordings that feature Dr. Dre before 2001 or after 2009.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” ILIKE ‘%dr. dre%’
AND ((“year” <= 2000) OR (“year” >= 2010))

40
Q

select all songs from 2013 that are not in the second or third rank

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year = 2013

AND year_rank NOT BETWEEN 2 AND 3

41
Q

find all songs in 2013 that do not contain Macklemore in the group

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year = 2013

AND “group” NOT ILIKE ‘%macklemore%’

42
Q

find all rows for 2013 where the artist is not empty

A

SELECT * FROM tutorial.billboard_top_100_year_end

WHERE year = 2013

AND artist IS NOT NULL

43
Q

Write a query that returns all rows for songs that were on the charts in 2013 and do not contain the letter “a”.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “song_name” NOT ILIKE ‘%a%’
AND “year” = 2013

44
Q

Write a query that returns all rows from 2010 ordered by rank, with artists ordered alphabetically for each song.

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year = 2012
ORDER BY “year_rank”, “artist”

45
Q

Name two different ways to comment out code in sql

A

–This comment won’t affect the way the code runs

/* Here’s a comment so long and descriptive that it could only fit on multiple lines. Fortunately, it, too, will not affect how this code runs. */

46
Q

Write a query that shows all rows for which T-Pain was a group member, ordered by rank on the charts, from lowest to highest rank (from 100 to 1).

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE “group” ILIKE ‘%t-pain%’
ORDER BY “year_rank” DESC

47
Q

Write a query that returns songs that ranked between 10 and 20 (inclusive) in 1993, 2003, or 2013. Order the results by year and rank, and leave a comment on each line of the WHERE clause to indicate what that line does

A

SELECT *
FROM tutorial.billboard_top_100_year_end
WHERE year_rank BETWEEN 10 AND 20 –selects beween 10 and 20
AND (year IN (1993,2003,2013)) – selects only 1993, 2003 and 2013
ORDER BY year, year_rank