Assignment Solutions: Basic SQL 1

Basic SQL Assignment Solutions
  • Author Avatar
    Written by:

    Nathan Rosidi

Take a look at the solutions and explanations to the Assignment: Basic SQL 1.

Hope you had fun solving the Basic SQL 1 questions and that they were not too difficult. Now we’ll go through every concept tested in the assignment, show you the solutions, and explain them.

Filtering Data Using One Condition in WHERE

The WHERE clause in SQL is used for filtering data before the aggregation. The query will return all the records that satisfy the condition stated in the WHERE clause.

These are the operators you can use in WHERE:

OperatorMeaning
=Equals
<Less than
>Greater than
<=Less than or equal
>= Greater than or equal
<>Does not equal
BETWEEN Between the specified range
LIKELooking for a pattern
INEquals the values listed in the parentheses

Note that the WHERE clause in PostgreSQL is case-sensitive.

Question 1

Find all inspections which are part of an inactive program

“Find all inspections which are part of an inactive program.”

Link to the question: https://platform.stratascratch.com/coding/10277-find-all-inspections-which-are-part-of-an-inactive-program

Data

The data you have here is in the table los_angeles_restaurant_health_inspections.

Table: los_angeles_restaurant_health_inspections
Loading Dataset

Solution

To write the solution, you need to select all the columns from the table and filter the output by the column program_status.

SELECT *
FROM los_angeles_restaurant_health_inspections
WHERE program_status = 'INACTIVE';
Missing data

Use the WHERE clause to show only data with the inactive program status. Since the WHERE clause is case-sensitive, the status name has to be written in all capitalized. That’s how it’s recorded in the source table.

You should get this output by running the code.

All required columns and the first 5 rows of the solution are shown

serial_numberactivity_datefacility_namescoregradeservice_codeservice_descriptionemployee_idfacility_addressfacility_cityfacility_idfacility_statefacility_zipowner_idowner_namepe_descriptionprogram_element_peprogram_nameprogram_statusrecord_id
DA2GQRJOS2017-03-07LAS MOLENDERAS97A1ROUTINE INSPECTIONEE00009972635 WHITTIER BLVDLOS ANGELESFA0160416CA90023OW0125379MARISOL FEREGRINORESTAURANT (0-30) SEATS HIGH RISK1632LAS MOLENDERASINACTIVEPR0148504
DAQZAULOI2017-10-11INTI PERUVIAN RESTAURANT94A1ROUTINE INSPECTIONEE00008285870 MELROSE AVE # #105LOS ANGELESFA0030334CA90038OW0023369MARIN & MARTINEZ GROUP CORP.RESTAURANT (31-60) SEATS HIGH RISK1635INTI PERUVIAN RESTAURANTINACTIVEPR0043182
DA0N7AWN02016-09-21MICHELLE'S DONUT HOUSE96A1ROUTINE INSPECTIONEE00007983783 S WESTERN AVELOS ANGELESFA0039310CA90018OW0032004SCOTT VICHETH KHEMRESTAURANT (0-30) SEATS MODERATE RISK1631MICHELLE'S DONUT HOUSEINACTIVEPR0031269
DA2M0ZPRD2017-01-24LA PRINCESITA MARKET95A1ROUTINE INSPECTIONEE00009972426 E 4TH STLOS ANGELESFA0065292CA90063OW0029496RAMIREZ FRANCISCOFOOD MKT RETAIL (25-1,999 SF) HIGH RISK1612LA PRINCESITA MARKETINACTIVEPR0027280
DAKIPC9UB2016-06-16LA PETITE BOULANGERIE86B1ROUTINE INSPECTIONEE0000721330 S HOPE STLOS ANGELESFA0180531CA90071OW0185889MARCO INVESTMENT CORP.RESTAURANT (31-60) SEATS MODERATE RISK1634LA PETITE BOULANGERIEINACTIVEPR0174307
DAH08SUQB2016-01-13CARNITAS URUAPAN - MARKET & BAKERY92A1ROUTINE INSPECTIONEE00007402102 E CESAR E CHAVEZ AVELOS ANGELESFA0158671CA90033OW0123723THE DRAWDE TOWER CORPORATIONFOOD MKT RETAIL (2,000+ SF) HIGH RISK1615CARNITAS URUAPAN MARKET AND BAKERYINACTIVEPR0146749
DA0KUBBF62015-08-13ANTOJITOS LISETH90A1ROUTINE INSPECTIONEE00006993324 W 6TH ST STE E1LOS ANGELESFA0006875CA90020OW0010254DOMINGUEZ, ELIZABETHRESTAURANT (0-30) SEATS HIGH RISK1632ANTOJITOS LISETHINACTIVEPR0030175
DAKYQHFDV2016-08-09MIKE'S DELI #294A1ROUTINE INSPECTIONEE0001058238 E 1ST STLOS ANGELESFA0066945CA90012OW0024459MIKE'S DELI INC.RESTAURANT (0-30) SEATS MODERATE RISK1631MIKE'S DELI #2INACTIVEPR0030530
DAH9FUPBP2017-01-31NISSI BAKERY90A1ROUTINE INSPECTIONEE00010061900 BEVERLY BLVD # ALOS ANGELESFA0166417CA90057OW0129565PEDRO MARCELINORESTAURANT (0-30) SEATS MODERATE RISK1631NISSI BAKERYINACTIVEPR0156731
DAERHLUYY2015-07-28GUI IL BUNJI75C1ROUTINE INSPECTIONEE00009463055 W 7TH ST STE CLOS ANGELESFA0016000CA90006OW0025575NAM, KWANG BRESTAURANT (61-150) SEATS HIGH RISK1638GUI IL BUNJIINACTIVEPR0006335
DATERW8EJ2015-12-11STK/BAGATELLE90A1ROUTINE INSPECTIONEE0000957755 N LA CIENEGA BLVDLOS ANGELESFA0049644CA90069OW0005262BRIDGE HOSPITALITY, LLCRESTAURANT (151 + ) SEATS HIGH RISK1641STK/BAGATELLEINACTIVEPR0044195
DA43D5RLO2017-01-09HOLLYWOOD ROCK CAFE80B1ROUTINE INSPECTIONEE00008226511 HOLLYWOOD BLVDLOS ANGELESFA0164076CA90028OW0127540HOLLYWOOD ROCK CAFE LLCRESTAURANT (0-30) SEATS MODERATE RISK1631HOLLYWOOD ROCK CAFEINACTIVEPR0152338
DATKE9ZEY2016-01-19MR BOBA95A1ROUTINE INSPECTIONEE00008693805 W 6TH STLOS ANGELESFA0179880CA90020OW0185360MR BOBA INCRESTAURANT (31-60) SEATS MODERATE RISK1634MR BOBAINACTIVEPR0173547
DAAZEJORN2015-11-06FIX COFFEE98A1ROUTINE INSPECTIONEE00009232100 ECHO PARK AVELOS ANGELESFA0062112CA90026OW0011918FIX COFFEE LLCRESTAURANT (31-60) SEATS MODERATE RISK1634FIX COFFEEINACTIVEPR0016777
DAL9QKQYF2016-08-10POPEYE DUMPLING AND NOODLES90A1ROUTINE INSPECTIONEE0000531709 S VERMONT AVELOS ANGELESFA0139085CA90005OW0102345CCC GALBI, INCRESTAURANT (0-30) SEATS HIGH RISK1632POPEYE DUMPLING AND NOODLESINACTIVEPR0123466
DA6TFCWEK2017-03-15LOUIS JUNIOR87B1ROUTINE INSPECTIONEE0001073601 E CENTURY BLVDLOS ANGELESFA0022896CA90002OW0022327LOUIS JR. INC.RESTAURANT (0-30) SEATS HIGH RISK1632LOUIS JUNIORINACTIVEPR0040285
DAHZWAM9W2015-12-01EL RINCON DE LAS DELICIAS95A1ROUTINE INSPECTIONEE00010255810 S NORMANDIE AVE STE #1BLOS ANGELESFA0012633CA90044OW0026880ORELLANA, MARIA SALOMERESTAURANT (0-30) SEATS HIGH RISK1632EL RINCON DE LAS DELICIASINACTIVEPR0000117
DANYAXWN12016-02-12HARRIS MARKET97A1ROUTINE INSPECTIONEE000019510624 JUNIPER STLOS ANGELESFA0027836CA90002OW0032589SHIELDS, BEVERLY LFOOD MKT RETAIL (25-1,999 SF) HIGH RISK1612HARRIS MARKETINACTIVEPR0035788
DAA0AJ0WC2016-07-12STARBUCKS COFFEE90A1ROUTINE INSPECTIONEE0000633300 WORLD WAYLOS ANGELESFA0049341CA90045OW0016382HOST INTERNATIONAL INCRESTAURANT (31-60) SEATS LOW RISK1633STARBUCKS COFFEEINACTIVEPR0007377
DAQOADCKZ2016-01-06BIBIGO-REMOTE STORAGE100A1ROUTINE INSPECTIONEE000037510250 SANTA MONICA BLVD SECIOLOS ANGELESFA0009488CA90067OW0008289CJ BAKERY INC.RESTAURANT (0-30) SEATS LOW RISK1630BIBIGO-REMOTE STORAGEINACTIVEPR0032144
DA0NJNGFD2017-08-04BROOKLYN PIZZA AND PASTA72C1ROUTINE INSPECTIONEE00007279228 W PICO BLVDLOS ANGELESFA0011004CA90035OW0017537JELLY FISH INCRESTAURANT (31-60) SEATS MODERATE RISK1634BROOKLYN PIZZA AND PASTAINACTIVEPR0002718
DA3YKNNH72016-06-20KAJU UDON86B1ROUTINE INSPECTIONEE00007224317 W BEVERLY BLVDLOS ANGELESFA0019591CA90004OW0004583BEVERLY WORLD INDUSTRIES INCRESTAURANT (0-30) SEATS HIGH RISK1632KAJU UDONINACTIVEPR0011187
DAH0MH5XB2015-08-31KAJU UDON73C1ROUTINE INSPECTIONEE00007224317 W BEVERLY BLVDLOS ANGELESFA0019591CA90004OW0004583BEVERLY WORLD INDUSTRIES INCRESTAURANT (0-30) SEATS HIGH RISK1632KAJU UDONINACTIVEPR0011187
DAUH8FDSV2015-08-06STARBUCKS COFFEE95A1ROUTINE INSPECTIONEE0000633100 WORLD WAYLOS ANGELESFA0049340CA90045OW0016382HOST INTERNATIONAL INCRESTAURANT (0-30) SEATS LOW RISK1630STARBUCKS COFFEEINACTIVEPR0038894
DAIA7KS012017-02-28SLAUSON FISH MARKET74C1ROUTINE INSPECTIONEE00007293420 W SLAUSON AVE STE HLOS ANGELESFA0046732CA90043OW0017620JI, KIL HOFOOD MKT RETAIL (25-1,999 SF) HIGH RISK1612SLAUSON FISH MARKETINACTIVEPR0024863
DAJ6DGZIM2015-09-10SILVER LAKE JUICE BAR90A1ROUTINE INSPECTIONEE00009232813 W SUNSET BLVDLOS ANGELESFA0048476CA90026OW0017618JI, BABARESTAURANT (31-60) SEATS MODERATE RISK1634SILVER LAKE JUICE BARINACTIVEPR0015112

Finding the Unique Values and Filtering Data Using More Than One Condition in WHERE

The DISTINCT statement returns only the unique values from the table. In other words, the duplicate rows will appear only once.

When there’s a need to filter data on more than one criteria, the keyword that needs to be used is AND.

State the first criteria in the WHERE clause, then write AND to write the second criteria, and so on. You’ll get a chain of criteria separated by the AND keyword. Note that you write WHERE only once.

Also, apart from using the AND operator for filtering on two or more criteria, you can use OR and NOT operators.

Question 2

London Olympic Swimmers

“Find the athletes who competed in swimming events at the London Olympics.”

Link to the question: https://platform.stratascratch.com/coding/9934-london-olympic-swimmers

Data

The question gives you the table olympics_athletes_events to work with.

Table: olympics_athletes_events
Loading Dataset

Solution

The athletes can compete more than once in the Olympics, so it’s wise to assume the athletes are showing more than once in the table. Use DISTINCT to show the athletes only once.

The output has to be filtered to show only swimming events, and the city should be London.

SELECT DISTINCT name
FROM olympics_athletes_events
WHERE sport = 'Swimming'
  AND city = 'London';
Missing data

Run the code to get the output.

All required columns and the first 5 rows of the solution are shown

name
Jeong Won-Yong
Leslie George Rich
Samuel Sam Blatherwick
Theodora Drakou

Filtering Out the NULL Values and Sorting Data

Filtering Out the NULL Values and Sorting Data

The output will not show the NULL values when the column specified in the WHERE clause is followed by ‘IS NOT NULL’.

If you want to show only the NULL values, then you have to write ‘IS NULL’.

The code output is sorted using the ORDER BY clause, where you explicitly state the column to be used for ordering data. After the column name, use the ASC keyword to sort data in ascending order. For descending order, use the keyword DESC.

It’s also possible to sort data by more than one column. To do that, simply separate the columns in the ORDER BY clause with a comma.

Question 3

Find all searches for San Francisco with a flexible cancellation policy and a review score rating

“Find all searches for San Francisco with a flexible cancellation policy and a review score rating. Sort the results by the review score in the descending order.”

Link to the question: https://platform.stratascratch.com/coding/9621-find-all-searches-for-san-francisco-with-a-flexible-cancellation-policy-and-a-review-score-rating

Data

There’s again one table: airbnb_search_details.

Table: airbnb_search_details
Loading Dataset

Solution

You need to show all the columns in the SELECT statement.

There will be three filtering criteria in the WHERE clause: the city is San Francisco, the cancellation policy is flexible, and there has to be a review score rating.

When looking for data that is not NULL. The command that will return non-NULL values is ‘IS NOT NULL’.

Finally, the output has to be in descending order according to the review scores rating.

SELECT *
FROM airbnb_search_details
WHERE city='SF'
  AND cancellation_policy='flexible'
  AND review_scores_rating IS NOT NULL
ORDER BY review_scores_rating DESC;
Missing data

The result is only one row.

All required columns and the first 5 rows of the solution are shown

idpriceproperty_typeroom_typeamenitiesaccommodatesbathroomsbed_typecancellation_policycleaning_feecityhost_identity_verifiedhost_response_ratehost_sinceneighbourhoodnumber_of_reviewsreview_scores_ratingzipcodebedroomsbeds
13276443470.05HousePrivate room{"Cable TV",Internet,"Wireless Internet","Air conditioning",Kitchen,"Indoor fireplace",Washer,"Smoke detector","Carbon monoxide detector","First aid kit",Shampoo}21Real BedflexibleFALSESFt2013-05-15Russian Hill21009410911

Using LIMIT to Filter Data

One way of filtering data in the output is using the LIMIT keyword. Unlike the WHERE clause, LIMIT doesn’t filter data based on its value.

The purpose of LIMIT is to limit the output to a certain number of rows.

Question 4

Hour Of Highest Gas Expense

“Find the hour with the highest gasoline cost. Assume there's only 1 hour with the highest gas cost.”

Link to the question: https://platform.stratascratch.com/coding/10005-hour-of-highest-gas-expense

Data

The table is lyft_rides with five columns.

Table: lyft_rides
Loading Dataset

Solution

The query will select only the column hour from the table. If data is ordered by the gasoline cost in descending order, the output will show hours sorted from the highest to the lowest gas price.

To show only the highest price, you need to show one row and there it is.

SELECT hour
FROM lyft_rides
ORDER BY gasoline_cost DESC
LIMIT 1;
Missing data

The answer to the question is the 10th hour.

All required columns and the first 5 rows of the solution are shown

hour
10

LIKE & ILIKE in the WHERE Clause

LIKE is a standard SQL operator used in the WHERE clause to find the patterns in values. What does that mean? All the previous operators used in WHERE were looking for the exact values. When you use LIKE, you’re looking for something that is, well, like something. For example, when looking for only a specific word or a particular letter appearing in the whole text.

The ILIKE operator is PostgreSQL-specific. It works the same way as LIKE; only it’s not case-sensitive.

The wildcard characters are also used with those operators since they help look for a part of the value. Most often, it’s '%' wildcard characters used in the following way.

ExampleDescription
'%word'Finds values starting with 'word'
'word%'Finds values ending with 'word'
'%word%'Finds values containing 'word' in any position

Question 5

Find drafts which contains the word 'optimism'

“Find drafts which contain the word 'optimism'.”

Link to the question: https://platform.stratascratch.com/coding/9805-find-drafts-which-contains-the-word-optimism

Data

The data is stored in the table google_file_store.

Table: google_file_store
Loading Dataset

Solution

Select all the columns from the table. The file name has to contain the word 'draft'. We know if the file is a draft, it will begin with the word 'draft'. Therefore, we need to put the wildcard character only at the end.

Also, the contents have to contain the word 'optimism'. It can be in the beginning, at the end, or in the middle of the content. Because of that, the wildcard is needed on both sides of the filtering criteria.

SELECT *
FROM google_file_store
WHERE filename ILIKE 'draft%'
  AND contents ILIKE '%optimism%';
Missing data

The output will be only one draft.

All required columns and the first 5 rows of the solution are shown

filenamecontents
draft2.txtThe stock exchange predicts a bull market which would make many investors happy, but analysts warn of possibility of too much optimism and that in fact we are awaiting a bear market.

Using WHERE With IN

Using WHERE With IN

The IN operator is shorthand for multiple OR operators. The list of criteria is listed in the parentheses, and the where clause looks if the value matches one of those in the list.

Question 6

Find songs that are ranked between 8-10

“Find songs that are ranked between 8-10.

Output the track name along with the corresponding position ordered ascendingly.”

Link to the question: https://platform.stratascratch.com/coding/9999-find-songs-that-are-ranked-between-8-10

Data

The table you’ll work with is spotify_worldwide_daily_song_ranking.

Table: spotify_worldwide_daily_song_ranking
Loading Dataset

Solution

First, select the track name and the position. We’re looking for songs in the 8th, 9th, or 10th position. These criteria are written in the parentheses after the IN operator.

SELECT trackname,
       position
FROM spotify_worldwide_daily_song_ranking
WHERE POSITION IN (8,
                   9,
                   10)
ORDER BY position;
Missing data

Here are some of the songs that comprise the output.

All required columns and the first 5 rows of the solution are shown

tracknameposition
Attention8
Alone8
Make Me (Cry)8
Perfect Duet (Ed Sheeran & Beyonce)8
Una Lady Como Tu8
Castle on the Hill9
Una Lady Como Tu9
Scared to Be Lonely9
Mobali9
Chocolat (feat. Awa)9
Bling Bling10
You Don't Know Me - Radio Edit10
Vacaciones10
El Party10
Ahora Dice10

Aggregate Functions & GROUP BY

The aggregate functions are the types of functions in SQL that perform calculations on a set of rows and return only one value.

The most commonly used aggregate functions are

  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()

The aggregate functions are commonly used with the GROUP BY clause. Using it is necessary whenever there’s any other column in the SELECT statement apart from the aggregate function. In other words, all these columns (except the aggregate function) have to be listed in the GROUP BY clause. Otherwise, it will throw an error.

Note that the columns that appear in the GROUP BY don’t have to appear in the SELECT statement. This means you can group by the columns, but it doesn’t mean you must show these columns in the output.

Question 7

Finding Updated Records

“We have a table with employees and their salaries, however, some of the records are old and contain outdated salary information. Find the current salary of each employee assuming that salaries increase each year. Output their id, first name, last name, department ID, and current salary. Order your list by employee ID in ascending order.”

Link to the question: https://platform.stratascratch.com/coding/10299-finding-updated-records

Data

Here, you’ll work with the table ms_employee_salary.

Table: ms_employee_salary
Loading Dataset

Solution

In the query, select all the columns except the salary. To find the highest salary, use the MAX() function. Data needs to be grouped by all the columns in the SELECT statement, except for the aggregate function.

Finally, data is ordered by the employee’s ID from lowest to highest.

SELECT id,
       first_name,
       last_name,
       department_id,
       max(salary) AS max_salary
FROM ms_employee_salary
GROUP BY id,
         first_name,
         last_name,
         department_id
ORDER BY id ASC;
Missing data

The query will return the following data.

All required columns and the first 5 rows of the solution are shown

idfirst_namelast_namedepartment_idsalary
1ToddWilson1006110000
2JustinSimon1005130000
3KellyRosario100242689
4PatriciaPowell1004170000
5SherryGolden100244101
6NatashaSwanson100590000
7DianeGordon100274591
8MercedesRodriguez100561048
9ChristyMitchell1001150000
10SeanCrawford1006190000
11KevinTownsend1002166861
12JoshuaJohnson1004123082
13JulieSanchez1001210000
14JohnColeman1001152434
15AnthonyValdez100196898
16BrianaRivas1005151668
17JasonBurnett100642525
18JeffreyHarris100220000
19MichaelRamsey100363159
20CodyGonzalez1004112809
21StephenBerry1002123617
22BrittanyScott1002162537
23AngelaWilliams1004100875
24WilliamFlores1003142674
25PamelaMatthews100557944
26AllisonJohnson1001128782
27AnthonyBall100334386
28AlexisBeck100512260
29JasonOlsen100651937
30StephenSmith1001194791
31KimberlyBrooks100395327
32EricZimmerman100683093
33PeterHolt100269945
34JustinDunn100367992
35JohnBall100447795
36JesusWard100536078
37PhilipGillespie100636424
38NicoleLewis1001114079
39LindaClark1002186781
40ColleenCarrillo1004147723
41JohnGeorge100121642
42TraciWilliams1003180000
43JosephRogers100522800
44TrevorCarter100138670
45KevinDuncan100345210
46JoshuaEwing100373088
47KimberlyDean100371416
48RobertLynch1004117960
49AmberHarding100277764
50VictoriaWilson1002176620
51TheresaEverett100231404
52KaraSmith1004192838
53TeresaCohen100198860
54WesleyTucker100590221
55MichaelMorris1005106799
56RachaelWilliams1002103585
57PatriciaHarmon1005147417
58EdwardSharp100541077
59KevinRobinson1005100924
60CharlesPearson1004173317
61RyanBrown1003120000
62DaleHayes100597662
63RichardSanford1001136083
64DanielleWilliams1006120000
65DeborahMartin100467389
66DustinBush100447567
67TylerGreen1002111085
68AntonioCarpenter100283684
69ErnestPeterson1005115993
70KarenFernandez1003101238
71KristineCasey100367651
72ChristineFrye1004137244
73WilliamPreston1003155225
74RichardCole1003180361
75JuliaRamos1006105000
76MonicaNguyen100260000
76MonicaNguyen100482000

Filtering Data Using HAVING

The HAVING clause has the same purpose as WHERE; it filters data. What you do in the where clause, you can do the same in the HAVING clause.

However, they are not the same. The HAVING clause is used for filtering data after the aggregation, whereas the WHERE clause filters data before the aggregation.This is also reflected in the code: WHERE is written before GROUP BY, while HAVING comes after GROUP BY.

Also, the aggregate functions are allowed in the HAVING clause. In WHERE they are not.

Question 8

3 Bed Minimum

“Find the average number of beds in each neighborhood that has at least 3 beds in total.

Output results along with the neighborhood name and sort the results based on the number of average beds in descending order.”

Link to the question: https://platform.stratascratch.com/coding/9627-3-bed-minimum

Data

This question, too, uses only one table: airbnb_search_details.

Table: airbnb_search_details
Loading Dataset

Solution

In the solution, you should select the column neighborhood and then use the AVG() function to calculate the average number of beds by neighborhood.

The output has to show only the neighborhood with at least 3 beds, so this criteria has to appear in the HAVING clause. To get the number of beds, use the SUM() function.

Finally, order data from the highest to the lowest average number of beds.

SELECT neighbourhood,
       AVG(beds) AS n_beds_avg
FROM airbnb_search_details
GROUP BY neighbourhood
HAVING SUM(beds) >= 3
ORDER BY n_beds_avg DESC;
Missing data

Here’s the solution output.

All required columns and the first 5 rows of the solution are shown

neighbourhoodn_beds_avg
Pacific Palisades6
Redondo Beach5
Windsor Terrace5
Hayes Valley5
Hell's Kitchen4
Rancho Palos Verdes4
The Rockaways4
Manhattan Beach4
Temple City4
Long Beach3.33
Astoria3.33
Westlake3.17
Cow Hollow3
West Los Angeles3
Lincoln Park3
Topanga3
Malibu2.5
Alphabet City2
East Village2
Loop2
Mid-Wilshire1.67
Bedford-Stuyvesant1.67
East Harlem1.67
Harlem1.58
Bernal Heights1.5
Studio City1.5
Williamsburg1.33
Dorchester1

Share