SQL Interview Questions
SQL Interview Questions
Select Queries
Select query for a specific columns
SELECT column, another_column, …
FROM mytable;
BETWEEN … AND … Number is within range of two values (inclusive) col_name BETWEEN 1.5 AND 10.5
NOT BETWEEN … AND … Number is not within range of two values (inclusive) col_name NOT BETWEEN 1 AND 10
NOT IN (…) Number does not exist in a list col_name NOT IN (1, 3, 5)
Q2. Find the movies released in the years between 2000 and 2010.
SELECT * FROM Movies WHERE Year BETWEEN 2000 AND 2010;
Q3. Find the movies not released in the years between 2000 and 2010.
SELECT * FROM Movies WHERE Year NOT BETWEEN 2000 AND 2010;
Q4. Find the first 5 Pixar movies and their release year.
= Case sensitive exact string comparison (notice the single equals) col_name = "abc"
NOT LIKE Case insensitive exact string inequality comparison col_name NOT LIKE "ABCD"
% Used anywhere in a string to match a sequence of zero or more characters (only col_name LIKE "%AT%"
with LIKE or NOT LIKE) (matches "AT", "ATTIC", "CAT" or even "BATS")
_ Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) col_name LIKE "AN_"
(matches "AND", but not "AN")
NOT IN (…) String does not exist in a list col_name NOT IN ("D", "E", "F")
Q3. Find all the movies (and director) not directed by John Lasseter
Q2. List the last four Pixar movies released (ordered from most recent to least)
Q2. Order all the cities in the United States by their latitude from north to south
SELECT * FROM North_american_cities WHERE Country = "United States" ORDER BY Latitude DESC;
Q3. List all the cities west of Chicago, ordered from west to east
SELECT * FROM North_american_cities WHERE Longitude < -87.69 ORDER BY Longitude ASC;
SELECT * FROM North_american_cities WHERE Country LIKE "Mexico" ORDER BY Population DESC
LIMIT 2;
Q5. List the third and fourth largest cities (by population) in the United States and their population
SELECT * FROM North_american_cities WHERE Country LIKE "United States" ORDER BY Population
DESC LIMIT 2 OFFSET 2;
E. Multi-table queries with JOINs
Select query with INNER JOIN on multiple tables:
Q1. Find the domestic and international sales for each movie
Q2. Show the sales numbers for each movie that did better internationally rather than
domestically.
SELECT title FROM movies join Boxoffice on movies.id = Boxoffice.Movie_id order by rating desc;
F. OUTER JOIN
Select query with LEFT/RIGHT/FULL JOINs on multiple tables:
Q3. List all buildings and the distinct employee roles in each building (including empty buildings)
SELECT DISTINCT Building_name, Role FROM Buildings LEFT JOIN employees ON building_name =
building;
G. A short note on NULLs
Select query with constraints on NULL values:
Q1. Find the name and role of all employees who have not been assigned to a building.
SELECT * FROM Employees LEFT JOIN Buildings ON Building_name = Building WHERE Building IS
NULL;
SELECT * FROM Buildings LEFT JOIN Employees ON Building_name = Building WHERE Building IS
NULL;
Q1. List all movies and their combined sales in millions of dollars
SELECT Title, Rating*10 as Percent FROM Movies LEFT JOIN Boxoffice ON Id=Movie_Id;
Q3. List all movies that were released on even number years
SELECT Title, Year FROM Movies LEFT JOIN Boxoffice ON Id=Movie_Id WHERE Year % 2 = 0;
Or
Q1. Find the longest time that an employee has been at the building
Q2. For each role, find the average number of years employed by employees in that role
Q3. Find the total number of employee years worked in each building
Q1. Find the number of Artists in the studio (without a HAVING clause)
FROM Employees
GROUP BY Role;
-- Find the total number of years employed by all Engineers
FROM Employees
GROUP BY Role
SELECT *, COUNT(Title)
FROM Movies
GROUP BY Director;
-- Find the total domestic and international sales that can be attributed to each director
FROM Movies
GROUP BY Director;
-- Add the studio's new production, Toy Story 4 to the list of movies (you can use any director)
-- Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million
domestically and 270 million internationally. Add the record to the BoxOffice table.
UPDATE Movies
WHERE Id = 2;
-- The year that Toy Story 2 was released is incorrect, it was actually released in 1999
UPDATE Movies
WHERE Id = 3;
-- Both the title and directory for Toy Story 8 is incorrect! The title should be "Toy Story 3" and it was
directed by Lee Unkrich
UPDATE Movies
WHERE Id = 11;
-- This database is getting too big, lets remove all movies that were released before 2005.
-- Andrew Stanton has also left the studio, so please remove all movies directed by him.
-- 3. Download_count An integer count of the number of times this database was downloaded
Name TEXT,
Version FLOAT,
Download_Count INTEGER);
-- Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was
released in.
-- Add another column named Language with a TEXT data type to store the language that the movie
was released in. Ensure that the default for this language is English.
-- We've sadly reached the end of our lessons, lets clean up by removing the Movies table