Assignment Solutions: Basic SQL 1

Basic SQL Assignment Solutions


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
serial_numberactivity_datefacility_namescoregradeservice_codeservice_descriptionemployee_idfacility_addressfacility_cityfacility_idfacility_statefacility_zipowner_idowner_namepe_descriptionprogram_element_peprogram_nameprogram_statusrecord_id
DAQHRSETQ2017-06-08MARGARITAS CAFE93A1ROUTINE INSPECTIONEE00000065026 S CRENSHAW BLVDLOS ANGELESFA0023656CA90043OW0004133BAZAN, ASCENCIONRESTAURANT (61-150) SEATS HIGH RISK1638MARGARITAS CAFEACTIVEPR0011718
DA2GQRJOS2017-03-07LAS MOLENDERAS97A1ROUTINE INSPECTIONEE00009972635 WHITTIER BLVDLOS ANGELESFA0160416CA90023OW0125379MARISOL FEREGRINORESTAURANT (0-30) SEATS HIGH RISK1632LAS MOLENDERASINACTIVEPR0148504
DAMQTA46T2016-03-22SANDRA'S TAMALES93A1ROUTINE INSPECTIONEE00010495390 WHITTIER BLVDLOS ANGELESFA0171769CA90022-4032OW0178828SANDRA'S TAMALES INC.RESTAURANT (0-30) SEATS MODERATE RISK1631SANDRA'S TAMALESACTIVEPR0164225
DAXMBTIRZ2018-02-12CAFE GRATITUDE97A1ROUTINE INSPECTIONEE0000828639 N LARCHMONT BLVD STE #102LOS ANGELESFA0058921CA90004OW0005704CAFE GRATITUDE LARCHMONT LLCRESTAURANT (61-150) SEATS HIGH RISK1638CAFE GRATITUDEACTIVEPR0019854
DAK8TBMS02015-09-10THE WAFFLE90A1ROUTINE INSPECTIONEE00007096255 W SUNSET BLVD STE #105LOS ANGELESFA0051830CA90028OW0035796THE WAFFLE, LLCRESTAURANT (61-150) SEATS HIGH RISK1638THE WAFFLEACTIVEPR0010922

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';

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

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
idnamesexageheightweightteamnocgamesyearseasoncitysporteventmedal
3520Guillermo J. AmparanMMexicoMEX1924 Summer1924SummerParisAthleticsAthletics Men's 800 metres
35394Henry John FinchettMGreat BritainGBR1924 Summer1924SummerParisGymnasticsGymnastics Men's Rings
21918Georg Frederik Ahrensborg ClausenM28DenmarkDEN1924 Summer1924SummerParisCyclingCycling Men's Road Race Individual
110345Marinus Cornelis Dick SigmondM26NetherlandsNED1924 Summer1924SummerParisFootballFootball Men's Football
54193Thodore Tho JeitzM26LuxembourgLUX1924 Summer1924SummerParisGymnasticsGymnastics Men's Individual All-Around

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';

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
idpriceproperty_typeroom_typeamenitiesaccommodatesbathroomsbed_typecancellation_policycleaning_feecityhost_identity_verifiedhost_response_ratehost_sinceneighbourhoodnumber_of_reviewsreview_scores_ratingzipcodebedroomsbeds
12513361555.68ApartmentEntire home/apt{TV,"Wireless Internet","Air conditioning","Smoke detector","Carbon monoxide detector",Essentials,"Lock on bedroom door",Hangers,Iron}21Real BedflexibleFALSENYCt89%2015-11-18East Harlem3871002901
7196412366.36CabinPrivate room{"Wireless Internet",Kitchen,Washer,Dryer,"Smoke detector","First aid kit","Fire extinguisher",Essentials,"Hair dryer","translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}23Real BedmoderateFALSELAf100%2016-09-10Valley Glen14919160611
16333776482.83HousePrivate room{TV,"Cable TV",Internet,"Wireless Internet",Kitchen,"Free parking on premises","Pets live on this property",Dog(s),"Indoor fireplace","Buzzer/wireless intercom",Heating,Washer,Dryer,"Smoke detector","Carbon monoxide detector","First aid kit","Safety card","Fire extinguisher",Essentials,Shampoo,"24-hour check-in",Hangers,"Hair dryer",Iron,"Laptop friendly workspace","translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50","Self Check-In",Lockbox}21Real BedstrictTRUESFt100%2013-12-26Richmond District117969411811
1786412448.86ApartmentPrivate room{"Wireless Internet","Air conditioning",Kitchen,Heating,"Suitable for events","Smoke detector","Carbon monoxide detector","First aid kit","Fire extinguisher",Essentials,Shampoo,"Lock on bedroom door",Hangers,"translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}21Real BedstrictTRUENYCt93%2010-05-11Williamsburg8861121111
14575777506.89VillaPrivate room{TV,Internet,"Wireless Internet","Air conditioning",Kitchen,"Free parking on premises",Essentials,Shampoo,"translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}62Real BedstrictTRUELAt70%2015-10-2221009070333

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;

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
indexweatherhourtravel_distancegasoline_cost
0cloudy724.4681.129
1cloudy2323.6671.993
2sunny1720.9310.859
3rainy229.5750.848
4rainy716.1110.952

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;

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
filenamecontents
draft1.txtThe stock exchange predicts a bull market which would make many investors happy.
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.
final.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. As always predicting the future market is an uncertain game and all investors should follow their instincts and best practices.

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%';

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
idpositiontracknameartiststreamsurldateregion
30365152Heart Won't ForgetMatoma28047https://open.spotify.com/track/2of2DM5LqTh7ohmmVXUKsH2017-02-04no
85559160Someone In The Crowd - From "La La Land" SoundtrackEmma Stone17134https://open.spotify.com/track/7xE4vKvjqUTtHyJ9zi0k1q2017-02-26fr
1046089175The GreatestSia10060https://open.spotify.com/track/7xHWNBFm6ObGEQPaUxHuKO2017-03-06cl
35082425UnforgettableFrench Montana46603https://open.spotify.com/track/3B54sVLJ402zGa6Xm4YGNe2017-10-01no
7768221Bad and Boujee (feat. Lil Uzi Vert)Migos1823391https://open.spotify.com/track/4Km5HrUvYTaSUfiSGPJeQR2017-01-27us

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;

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

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
idfirst_namelast_namesalarydepartment_id
1ToddWilson1100001006
1ToddWilson1061191006
2JustinSimon1289221005
2JustinSimon1300001005
3KellyRosario426891002

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;

The query will return the following data.

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

idfirst_namelast_namedepartment_idmax
1ToddWilson1006110000
2JustinSimon1005130000
3KellyRosario100242689
4PatriciaPowell1004170000
5SherryGolden100244101

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
idpriceproperty_typeroom_typeamenitiesaccommodatesbathroomsbed_typecancellation_policycleaning_feecityhost_identity_verifiedhost_response_ratehost_sinceneighbourhoodnumber_of_reviewsreview_scores_ratingzipcodebedroomsbeds
12513361555.68ApartmentEntire home/apt{TV,"Wireless Internet","Air conditioning","Smoke detector","Carbon monoxide detector",Essentials,"Lock on bedroom door",Hangers,Iron}21Real BedflexibleFALSENYCt89%2015-11-18East Harlem3871002901
7196412366.36CabinPrivate room{"Wireless Internet",Kitchen,Washer,Dryer,"Smoke detector","First aid kit","Fire extinguisher",Essentials,"Hair dryer","translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}23Real BedmoderateFALSELAf100%2016-09-10Valley Glen14919160611
16333776482.83HousePrivate room{TV,"Cable TV",Internet,"Wireless Internet",Kitchen,"Free parking on premises","Pets live on this property",Dog(s),"Indoor fireplace","Buzzer/wireless intercom",Heating,Washer,Dryer,"Smoke detector","Carbon monoxide detector","First aid kit","Safety card","Fire extinguisher",Essentials,Shampoo,"24-hour check-in",Hangers,"Hair dryer",Iron,"Laptop friendly workspace","translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50","Self Check-In",Lockbox}21Real BedstrictTRUESFt100%2013-12-26Richmond District117969411811
1786412448.86ApartmentPrivate room{"Wireless Internet","Air conditioning",Kitchen,Heating,"Suitable for events","Smoke detector","Carbon monoxide detector","First aid kit","Fire extinguisher",Essentials,Shampoo,"Lock on bedroom door",Hangers,"translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}21Real BedstrictTRUENYCt93%2010-05-11Williamsburg8861121111
14575777506.89VillaPrivate room{TV,Internet,"Wireless Internet","Air conditioning",Kitchen,"Free parking on premises",Essentials,Shampoo,"translation missing: en.hosting_amenity_49","translation missing: en.hosting_amenity_50"}62Real BedstrictTRUELAt70%2015-10-2221009070333

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;

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

Basic SQL Assignment Solutions


Become a data expert. Subscribe to our newsletter.