SQL Practice Questions
Course 01: Course Name
Week 01: Selecting and Retrieving Data
Quiz 01
- This statement will return an error. Please list why.
SELECT TrackID Name AlbumID
FROM tracks
- It’s missing comma after
TrackID
, and Name
- It doesn’t state where to get the data from
- It lists too many columns
- When using SQLite, what datatypes can you assign to a column when creating a new table? Select all that apply.
- Real
- Integer
- Null
- Text
- Primary Keys must be unique values.
- True
- False
- What is the query below missing in order to execute?
- Select
- A Comma
- From
- The Column Names
Answers 01
Question |
Answer |
1 |
i |
2 |
i, ii, iv |
3 |
i |
4 |
i |
Quiz 02
- Select the jobs below that may use SQL in their work (select all that apply).
- Data Analyst
- Backend Developer
- Data Scientist
- QA Engineer
- DBA
- How does a data scientist and DBA differ in how they use SQL?
- DBAs manage the database for other users.
- Data scientists don’t write complex queries.
- DBA’s are the only ones who merge datasets together.
- Data scientists only query the database and don’t create tables.
- Which of the following statements are true of Entity Relationship (ER) Diagrams?
- They speed up your querying time.
- They show you the relationships between tables.
- They are usually a representation of a business process.
- They usually are represented in a visual format.
- They only represent entities in the diagram.
- They identify the Primary Keys
-
Select the query below that will retrieve all columns from the customers table.
RETRIEVE * FROM customers
-
SELECT
FirstName
,LastName
,Address
,City
,State
,ZipCode
,PhoneNumber
FROM customers
SELECT * FROM customers
SELECT (*) FROM customers
-
Select the query that will retrieve only the Customer First Name, Last Name, and Company.
-
SELECT FirstName LastName Company
FROM customers
-
SELECT FirstName LastName Company
FROM customers
-
-
SELECT FirstName, LastName, Company
FROM customers
- The ER diagram below is depicting what kind of relationship between the EMPLOYEES and CUSTOMERS tables?
- One-to-one
- One-to-many
- Many-to-one
- Many-to-many
- The data model depicted in the ER diagram below could be described as a ?
- Transactional Model
- Relational Model
- Star Schema
- When using the
CREATE TABLE
command and creating new columns for that table, which of the following statements is true?
- You must insert data into all the columns while creating the table
- You must assign a data type to each column
- You can create the table and then assign data types later
-
Look at the values in the two columns below. Based on the values in each column, which column could potentially be used as a primary key?
Column 1 |
Column 2 |
5 |
2 |
6 |
4 |
1 |
5 |
2 |
5 |
34 |
32 |
8 |
6 |
9 |
4 |
- Column 1
- Column 2
- Column 1 OR Column 2
- In order to retrieve data from a table with SQL, every SQL statement must contain?
WHERE
CREATE
FIND
SELECT
Answers 02
Question |
Answer |
1 |
all |
2 |
i |
3 |
ii, iii, iv, vi |
4 |
iii |
5 |
iv |
6 |
ii |
7 |
ii |
8 |
ii |
9 |
i |
10 |
iv |
Quiz 03
-
For all of the questions in this quiz, we are using the Chinook database. All of the interactive code blocks have been setup to retrieve data only from this database.
Retrieve all the records from the Employees table.
-
Retrieve the FirstName, LastName, Birthdate, Address, City, and State from the Employees table.
-
Retrieve all the columns from the Tracks table, but only return 20 rows.
Answers 03
-
-
SELECT FirstName, LastName, Birthdate, Address, City, State from Employees;
-
SELECT* FROM Tracks LIMIT 20;
Week 02: Filtering, Sorting and Math
Quiz 01
For all the questions in this practice set, you will be using the Salary by Job Range Table. This is a single table titled: salary_range_by_job_classification
. This table contains the following columns:
- SetID
- Job_Code
- Eff_Date
- Sal_End_Date
- Salary_setID
- Sal_Plan
- Grade
- Step
- Biweekly_High_Rate
- Biweekly_Low_Rate
- Union_Code
- Extended_Step
- Pay_Type
- Write the query to get distinct values for Extended_step.
- Write a query to get, excluding $0.00, the minimum Biweekly_High_Rate of pay.
- Query to get maximum Biweekly_High_Rate of pay.
- Query to get pay type for all the job codes that start with ‘03’.
- Query to find the Effective Date (eff_date) or Salary End Date (sal_end_date) for grade Q90H0.
- Sort the Biweekly low rate in ascending order.
- What Step are Job Codes 0110-0400.
- What is the Biweekly High Rate minus the Biweekly Low Rate for job Code 0170?
- What is the Extended Step for Pay Types M, H, and D?
- What is the step for Union Code 990 and a Set ID of SFMTA or COMMN?
Answers 01
-
SELECT DISTINCT Extended_step FROM salary_range_by_job_classification;
-
Select MIN(Biweekly_high_Rate)
From salary_range_by_job_classification
WHERE Biweekly_high_Rate <> '$0.00';
-
Select MAX(Biweekly_high_Rate)
From salary_range_by_job_classification;
-
SELECT job_code, pay_type
FROM salary_range_by_job_classification
WHERE Job_Code LIKE '03%';
-
Select grade, eff_date, sal_end_date
FROM salary_range_by_job_classification
wHERE grade = 'Q90H0';
-
SELECT Biweekly_Low_Rate
FROM salary_range_by_job_classification
ORDER BY Biweekly_Low_Rate ASC;
-
SELECT step, job_code FROM salary_range_by_job_classification
WHERE job_code BETWEEN '0110' AND '0400';
-
SELECT Biweekly_High_Rate - Biweekly_Low_Rate
FROM salary_range_by_job_classification
WHERE Job_Code = '0170';
-
SELECT Extended_Step
FROM salary_range_by_job_classification
WHERE Pay_Type IN ('M', 'H', 'D');
-
SELECT Extended_Step, Union_Code, SetID
FROM salary_range_by_job_classification
WHERE Union_Code = 990 AND SetID IN ('SFMTA', 'COMMN');
Quiz 02
- Filtering data is used to do which of the following? (select all that apply)
- Narrows down the results of the data.
- Reduce the time it takes to run the query
- Reduces the strain on the client application
- Helps you understand the contents of your data
- Removes unwanted data in a calculation
- You are doing an analysis on musicians that start with the letter “K”. Select the correct query that would retrieve only the artists whose name starts with this letter.
SELECT name FROM Artists WHERE name LIKE ‘K%’;
SELECT name FROM Artists WHERE name IN ‘K%’;
SELECT name FROM Artists WHERE name LIKE ‘%K’;
SELECT name FROM Artists WHERE name LIKE ‘%K%’;
- A null and a zero value effectively mean the same thing. True or false?
- True
- False
- Select all that are true regarding wildcards (Select all that apply.)
- Wildcards can be used for non-text data items
- Wildcards at the end of search patterns take longer to run
- Wildcards take longer to run compared to a logical operator
- Select the statements below that ARE NOT true of the ORDER BY clause (select all that apply).
- Cannot sort by a column not retrieved
- It’s only applied to the column names it directly precedes
- Can take the name of one or more columns
- Can be anywhere in the select statement
- Select all of the valid math operators in SQL (select all that apply).
- / (division)
-
- ^ (exponents)
-
-
- Which of the following is an aggregate function? (select all that apply)
- MAX()
- MIN()
- COUNT()
- DISTINCT()
- Which of the following is true of GROUP BY clauses? (Select all that apply.)
- NULLs will be grouped together if your Group By column contains NULLs
- GROUP BY clauses can contain multiple columns
-
Every column in your select statement may/can be present in a group by clause, except for aggregated calculations.
- Select the true statement below.
- WHERE filters after the data is grouped
- HAVING filters after the data is grouped.
- Which is the correct order of occurrence in a SQL statement?
- select, from, where, order by, having
- select, from, where, group by, having
- select, group by, from, where, having
- select, having, where, group by
Answers 02
Question |
Answer |
1 |
all |
2 |
i |
3 |
ii |
4 |
ii, iii |
5 |
i, iv |
6 |
i, ii, iv, v |
7 |
i, ii, iii |
8 |
i, ii, iii |
9 |
ii |
10 |
ii |
Quiz 03
All of the questions in this quiz refer to the open source Chinook Database. Please familiarize yourself with the ER diagram to familiarize yourself with the table and column names to write accurate queries and get the appropriate answers.
- Count of tracks that have a length of 5,000,000 milliseconds or more.
- Count of invoices whose total is between $5 and $15 dollars.
- Find all the customers from the following States: RJ, DF, AB, BC, CA, WA, NY.
- Find all the invoices for customer 56 and 58 where the total was between $1.00 and $5.00.
- Count of tracks whose name starts with ‘All’.
- Find all the customer emails that start with “J” and are from gmail.com.
- Find all the invoices from the billing city Brasília, Edmonton, and Vancouver and sort in descending order by invoice ID.
- Show the number of orders placed by each customer (hint: this is found in the invoices table) and sort the result by the number of orders in descending order.
- Find the albums with 12 or more tracks.
Answers 03
-
SELECT COUNT(TrackId) FROM Tracks WHERE MIlliseconds > 5000000;
-
SELECT COUNT(InvoiceId) FROM Invoices WHERE Total BETWEEN 5 AND 15;
-
SELECT FirstName, LastName, Company FROM Customers
WHERE STATE IN ('RJ', 'DF', 'AB', 'BC', 'CA', 'WA', 'NY');
-
SELECT * FROM Invoices WHERE CustomerId IN (56, 58) AND Total BETWEEN 1 AND 5;
-
SELECT COUNT(TrackId) FROM Tracks WHERE Name LIKE 'All%';
-
SELECT Email FROM Customers WHERE Email LIKE 'J%@gmail.com';
-
SELECT * FROM Invoices WHERE BillingCity IN ('Brasília', 'Edmonton', 'Vancouver') ORDER BY InvoiceId DESC;
-
SELECT CustomerId, COUNT(InvoiceId) AS total_items
FROM Invoices GROUP BY CustomerId ORDER BY total_items DESC;
-
SELECT AlbumId, COUNT(TrackId) as total_tracks
FROM Tracks GROUP BY AlbumId HAVING total_tracks >= 12;
Week 03: Advanced Joins: Left, Right, and Full Outer Joins
Quiz 01
All of the questions in this quiz pull from the open source Chinook Database. Please refer to the ER Diagram and familiarize yourself with the table and column names to write accurate queries and get the appropriate answers.
- How many albums does the artist Led Zeppelin have?
- Create a list of album titles and the Track Ids for the artist “Audioslave”.
- Find the first and last name of any customer who does not have an invoice. Are there any customers returned from the query?
- What is the total price for the album “Big Ones”? (assume each track costs $0.99)
Answers 01
-
SELECT COUNT(DISTINCT albums.Albumid)
FROM albums INNER JOIN artists
ON albums.Artistid = artists.Artistid
WHERE artists.Name = 'Led Zeppelin';
-
SELECT artists.Name, albums.Albumid, tracks.Trackid
FROM tracks INNER JOIN albums INNER JOIN artists
ON tracks.Albumid = albums.Albumid AND albums.Artistid = artists.Artistid
WHERE artists.Name = 'Audioslave';
-
SELECT FirstName, LastName
FROM customers LEFT JOIN invoices
ON customers.Customerid = invoices.Customerid
WHERE Invoiceid IS NULL;
-
SELECT albums.Albumid, albums.Title, COUNT(tracks.Trackid)
FROM tracks INNER JOIN albums
ON tracks.Albumid = albums.Albumid
WHERE albums.Title = 'Big Ones'
GROUP BY albums.Albumid, albums.Title;
Quiz 02
- Which of the following statements is true regarding subqueries?
- Subqueries will process whichever query you indicate for them to process first.
- Subqueries always process the innermost query first and the work outward.
- Subqueries always process the outermost query first and the work inward.
- If you can accomplish the same outcome with a join or a subquery, which one should you always choose?
- A join because they are always faster
- A subquery because they are always faster
- Whichever one you understand better and can write faster.
- Joins are usually faster, but subqueries can be more reliable, so it depends on your situation.
- The following diagram is a depiction of what type of join?
- Inner Join
- Left Join
- Right Join
- Full Outer Join
- Select which of the following statements are true regarding inner joins. (Select all that apply)
- Inner joins retrieve all matching and nonmatching rows from a table
- Inner joins are one of the most popular types of joins use
- There is no limit to the number of table you can join with an inner join.
- Performance will most likely worsen with the more joins you make
- Which of the following is true regarding Aliases? (Select all that apply.)
- Aliases are often used to make column names more readable.
- SQL aliases are used to give a table, or a column in a table, a temporary name.
- An alias only exists for the duration of the query.
-
What is wrong with the following query?
SELECT Customers.CustomerName, Orders.OrderID
FROM LEFT JOIN ON Customers.CustomerID = Orders.CustomerID FROM Orders AND Customers
ORDER BY
CustomerName;
- Should be using an inner join rather than a left join
- Column names do not have an alias
- The table name comes after the join condition
- What is the difference between a left join and a right join?
- There is actually no difference between a left and a right join.
- A right join is always used before a full outer join, whereas a left join is always used after a full outer join
- The only difference between a left and right join is the order in which the tables are relating.
- A left join always is used before a right join in a query statement
- If you perform a cartesian join on a table with 10 rows and a table with 20 rows, how many rows will there be in the output table?
- 200
- 20
- 10
- 15
- Which of the following statements about Unions is true? (select all that apply)
- Each SELECT statement within UNION must have the same number of columns
- The columns must also have similar data types
- The order of the SELECTed columns in a UNION does not matter
- The UNION operator is used to combine the result-set of two or more SELECT statements
- Data scientists need to use joins in order to: (select the best answer)
- Filter data from multiple tables.
- Retrieve data from multiple tables.
- Create new tables.
Answers 02
Question |
Answer |
1 |
2 |
2 |
4 |
3 |
1 |
4 |
2, 3, 4 |
5 |
all |
6 |
3 |
7 |
3 |
8 |
1 |
9 |
1, 2, 4 |
10 |
2 |
Quiz 03
- Using a subquery, find the names of all the tracks for the album “Californication”.
- Find the total number of invoices for each customer along with the customer’s full name, city and email.
- Retrieve the track name, album, artistID, and trackID for Trackid 12.
- Retrieve a list with the managers last name, and the last name of the employees who report to him or her
- Find the name and ID of the artists who do not have albums.
- Use a UNION to create a list of all the employee’s and customer’s first names and last names ordered by the last name in descending order.
- See if there are any customers who have a different city listed in their billing city versus their customer city.
Answers 03
-
SELECT Trackid, Name FROM Tracks
WHERE Albumid IN (SELECT Albumid FROM Albums
WHERE Title = 'Californication')
ORDER BY Trackid;
-
SELECT Customers.Customerid, FirstName, LastName, Email, COUNT(InvoiceId)
FROM Customers LEFT JOIN Invoices
ON Customers.Customerid = Invoices.Customerid
GROUP BY Customers.Customerid;
-
SELECT Tracks.Name, Albums.Title, Albums.ArtistID, Tracks.Trackid
FROM TRACKS INNER JOIN Albums
ON Tracks.Albumid = Albums.Albumid
WHERE Tracks.Trackid = 12;
-
SELECT a.EmployeeID as managerid, a.LastName as managerLastName,
b.EmployeeID as employeeid, b.LastName as employeeLastName
FROM EMPLOYEES a LEFT JOIN EMPLOYEES b
ON b.ReportsTo = a.EmployeeID
WHERE b.EmployeeID IS NOT NULL;
-
SELECT Artists.ArtistID, Artists.Name
FROM Artists LEFT JOIN Albums
ON Artists.Artistid = Albums.Artistid
WHERE Albums.Artistid IS NULL;
-
SELECT Customers.FirstName, Customers.LastName
FROM Customers
UNION
SELECT Employees.FirstName, Employees.LastName
FROM Employees
ORDER BY LastName DESC;
-
SELECT Customers.Customerid, Customers.City, Invoices.BillingCity
FROM Customers INNER JOIN Invoices
On Customers.Customerid = Invoices.Customerid
WHERE Customers.City <> Invoices.BillingCity;
Week 04: Strings, Date and Time
Quiz 01
- Which of the following are supported in SQL when dealing with strings? (Select all that apply)
- Upper
- Lower
- Trim
- Substring
- Concatenate
- What will the result of the statement be?
SELECT SUBSTR('You are beautiful.', 3)
- u are beautiful.
- You are beautiful.
- This will return an error
- beautiful.
- What are the results of the following query?
SELECT * orders WHERE order_date = ‘2017-07-15’
Additional information:
Orders = integer
Order_date = datetime
- You will get all the orders with an order date of 2017-07-15.
- You won’t get any results.
- You will get all of the orders.
- Case statements can only be used for which of the following statements (select all that apply)?
- Delete
- Select
- Update
- Insert
- Which of the following is FALSE regarding views?
- Views are stored in a query
- Views will remain after the database connection has ended
- Views can be used to encapsulate queries
- You are only allowed to have one condition in a case statement. True or false?
- True
- False
- Select the correct SQL syntax for creating a view.
CREATE VIEW AS SELECT * FROM customers WHERE Name LIKE '%I';
CREATE VIEW customers AS SELECT * FROM customers WHERE Name LIKE '%I';
INSERT VIEW customers AS Select * FROM customers WHERE Name LIKE '%I';
- Profiling data is helpful for which of the following? (Select all that apply)
- Joining tables together
- Filter out unwanted data elements
- Understanding your data
- What is the most important step before beginning to write queries?
- Understanding your data
- Deciding what tables you want to join
- Deciding what should be done on the client application vs the RDMS
- When debugging a query, what should you always remember to do first?
- Make sure you didn’t miss any commas.
- Start with the inner most query
- Start simple and break it down first
- Start by examining the joins
Answers 01
Question |
Answer |
1 |
all |
2 |
1 |
3 |
2 |
4 |
all |
5 |
2 |
6 |
2 |
7 |
2 |
8 |
2, 3 |
9 |
1 |
10 |
3 |
Quiz 02
- Pull a list of customer ids with the customer’s full name, and address, along with combining their city and country together. Be sure to make a space in between these two and make it UPPER CASE. (e.g. LOS ANGELES USA)
- Create a new employee user id by combining the first 4 letters of the employee’s first name with the first 2 letters of the employee’s last name. Make the new field lower case.
- Show a list of employees who have worked for the company for 15 or more years using the current date function. Sort by lastname ascending.
Answers 02
SELECT FirstName, LastName, UPPER(City || " " || Country) AS Location FROM Customers;
SELECT LOWER(SUBSTR(FirstName, 1, 4) || SUBSTR(LastName, 1, 2)) FROM Employees;
SELECT FirstName, LastName, STRFTIME('%Y', DATE('now')) - STRFTIME('%Y', HireDate) AS Experience FROM Employees WHERE STRFTIME('%Y', DATE('now')) - STRFTIME('%Y', HireDate) > 15 ORDER BY LastName ASC;