SQL Drills Flashcards

1
Q

Use a SELECT statement to get the first_name and
last_name of all employees in the hcm.employees table.

A

SELECT first_name, last_name
FROM hcm.employees;

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

Use a SELECT statement to get the last_name and city of
all customers from the oes.customers table. Alias the last_name to customer_last_name in
the query.

A

SELECT last_name AS customer_last_name, city
FROM oes.customers;

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

Use a SELECT statement to select all columns from the
oes.order_items table.

A

SELECT *
FROM oes.order_items;

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

Use a SELECT DISTINCT statement to get the distinct (i.e.
unique) values that occur in the locality column from the
bird.antarctic_populations table.

A

SELECT DISTINCT locality
FROM bird.antarctic_populations;

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

Use a SELECT DISTINCT statement to get the distinct
combinations of values for both the locality and
species_id columns from the bird.antarctic_populations
table.

A

SELECT DISTINCT locality, species_id
FROM bird.antarctic_populations;

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

Use the hcm.employees table

Write a query that returns all employees ordered
alphabetically by their last name from A to Z.

A

SELECT *
FROM hcm.employees
ORDER BY last_name;

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

Use the hcm.employees table

Write a query that returns all employees ordered by
salary from highest to lowest.

A

SELECT *
FROM hcm.employees
ORDER BY salary DESC;

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

Use the hcm.employees table

Write a query to return all employees ordered by most
recently hired to longest serving.

A

SELECT *
FROM hcm.employees
ORDER BY hire_date DESC;

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

Use the hcm.employees table

Write a query to return all employees ordered by
department_id in ascending order and within each
department_id, order by salary from highest to lowest.

A

SELECT *
FROM hcm.employees
ORDER BY department_id, salary DESC;

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

Use the hcm.employees table

Write a query to return the employee_id, first_name,
last_name and salary for the top 10 employees who get
paid the most.

A

SELECT TOP (10) employee_id, first_name, last_name, salary
FROM hcm.employees
ORDER BY salary DESC;

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

Use the hcm.employees table

Write a query to return the employee_id, first_name,
last_name and salary for the employee or employees
who get paid the least

A

SELECT TOP (1) WITH TIES employee_id, first_name, last_name, salary
FROM hcm.employees
ORDER BY salary ASC;

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

Write query to give the total number of employees in
each department as given by the department_id column
in the hcm.employees table.

A

select department_id, count(*) as employee_count
from hcm.employees
group by department_id

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

Write a query to give the average salary in each
department as given by the department_id column in the
hcm.employees table. Order the query result by average
salary from highest to lowest.

A

select department_id, avg(salary) as average_salary
from hcm.employees
group by department_id
order by average_salary desc

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

Write a query to give the total number of products on
hand at each warehouse as given by the warehouse_id
column in the oes.inventories table. Also, limit the result
to only warehouses which have greater than 5,000
product items on hand.

A

select warehouse_id, sum(quantity_on_hand) as total_products_on_hand
from oes.inventories
group by warehouse_id
having sum(quantity_on_hand) > 5000

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

What is the date of the most recent population count at
each locality in the bird.antarctic_populations table?

A

select locality, max(date_of_count) as most_recent_count_date
from bird.antarctic_populations
group by locality

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

What is the date of the most recent population count for each
species at each locality in the bird.antarctic_populations
table?

A

select species_id, locality, max(date_of_count) as most_recent_count_date
from bird.antarctic_populations
group by species_id, locality

17
Q

Use the hcm.employees table

Select employees from the hcm.employees table who live
in either Seattle or Sydney.

A

select *
from hcm.employees
where city = ‘Seattle’ OR city = ‘Sydney’

18
Q

Use the hcm.employees table

Select employees who live in any of the following cities:
- Seattle
- Sydney
- Ascot
- Hillston

A

select *
from hcm.employees
where city IN (‘Seattle’, ‘Sydney’, ‘Ascot’, ‘Hillston’)

19
Q

Use the hcm.employees table

Select employees from Sydney who have a salary greater
than $200,000.

A

select *
from hcm.employees
where city = ‘Sydney’ AND salary > 200000

20
Q

Use the hcm.employees table

Select employees who live in either Seattle or Sydney
and were also hired on or after 1st January 2019.

A

select *
from hcm.employees
where (city = ‘Seattle’ OR city = ‘Sydney’) AND hire_date > ‘2019-01-01’

21
Q

Use the oes.products table

Select products from the oes.products table which do
not have a product category_id of either 1, 2, or 5.

A

select *
from oes.products
where category_id NOT IN (1, 2, 5)

22
Q

Use the hcm.employees and hcm.departments tables

Write a query to return the following attributes for
employees who belong to a department:
- employee_id
- first_name
- last_name
- salary
- department_name

A

select e.employee_id, e.first_name, e.last_name, e.salary, d.department_name
from hcm.employees e INNER JOIN hcm.departments d
on e.department_id = d.department_id

23
Q

Use the hcm.employees and hcm.departments tables

Write a query to return the following attributes for all
employees, including employees who do not belong to a
department:
- employee_id
- first_name
- last_name
- salary
- department_name

A

select e.employee_id, e.first_name, e.last_name, e.salary, d.department_name
from hcm.employees e LEFT OUTER JOIN hcm.departments d
on e.department_id = d.department_id

24
Q

Use the hcm.employees and hcm.departments tables

Write a query to return the total number of employees in
each department. Include the department_name in the
query result. Also, include employees who have not been
assigned to a department.

A

select count(*) AS number_of_employees, d.department_name
from hcm.employees e LEFT OUTER JOIN hcm.departments d
on e.department_id = d.department_id
group by department_name

25
Q

Return all rows from both the bird.california_sightings
table and the bird.arizona_sightings table. Use column
names from the bird.california_sightings table.

A

SELECT sighting_id, common_name, scientific_name, location_of_sighting, sighting_date
FROM bird.california_sightings
UNION ALL
SELECT sighting_id, common_name, scientific_name, sighting_location, sighting_date
FROM bird.arizona_sightings;

26
Q

Return all unique species - as identified by the
scientific_name column – for species which have been
sighted in either California or Arizona. Use column names
from the bird.california_sightings table.

A

SELECT scientific_name
FROM bird.california_sightings
UNION
SELECT scientific_name
FROM bird.arizona_sightings;

27
Q

Return all unique combinations of species
(scientific_name) and state name. The state_name will
need to be added on as a new expression which gives
the applicable state name. Use column names from the
bird.california_sightings table. Order by state_name
and then by scientific_name in ascending order.

A

SELECT scientific_name, ‘California’ AS state_name
FROM bird.california_sightings
UNION
SELECT scientific_name, ‘Arizona’
FROM bird.arizona_sightings
ORDER BY state_name, scientific_name;

28
Q

Return all rows from all the bird sightings tables i.e.
Arizona, California and Florida. Use column names from
the bird.california_sightings table.

A

SELECT sighting_id, common_name, scientific_name, location_of_sighting, sighting_date, ‘California’ AS state_name
FROM bird.california_sightings
UNION ALL
SELECT sighting_id, common_name, scientific_name, sighting_location, sighting_date, ‘Arizona’
FROM bird.arizona_sightings
UNION ALL
SELECT observation_id, NULL AS common_name, scientific_name, locality, sighting_datetime, ‘Florida’
FROM bird.florida_sightings;

29
Q

Return all unique customer ids for customers who have
placed orders.

A

SELECT customer_id
FROM oes.customers
INTERSECT
SELECT customer_id
FROM oes.orders;

30
Q

Return all unique product ids for products that are
currently not in stock.

A

SELECT product_id
FROM oes.products
EXCEPT
SELECT product_id
FROM oes.inventories;