SQL Drills Flashcards
Use a SELECT statement to get the first_name and
last_name of all employees in the hcm.employees table.
SELECT first_name, last_name
FROM hcm.employees;
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.
SELECT last_name AS customer_last_name, city
FROM oes.customers;
Use a SELECT statement to select all columns from the
oes.order_items table.
SELECT *
FROM oes.order_items;
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.
SELECT DISTINCT locality
FROM bird.antarctic_populations;
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.
SELECT DISTINCT locality, species_id
FROM bird.antarctic_populations;
Use the hcm.employees table
Write a query that returns all employees ordered
alphabetically by their last name from A to Z.
SELECT *
FROM hcm.employees
ORDER BY last_name;
Use the hcm.employees table
Write a query that returns all employees ordered by
salary from highest to lowest.
SELECT *
FROM hcm.employees
ORDER BY salary DESC;
Use the hcm.employees table
Write a query to return all employees ordered by most
recently hired to longest serving.
SELECT *
FROM hcm.employees
ORDER BY hire_date DESC;
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.
SELECT *
FROM hcm.employees
ORDER BY department_id, salary DESC;
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.
SELECT TOP (10) employee_id, first_name, last_name, salary
FROM hcm.employees
ORDER BY salary DESC;
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
SELECT TOP (1) WITH TIES employee_id, first_name, last_name, salary
FROM hcm.employees
ORDER BY salary ASC;
Write query to give the total number of employees in
each department as given by the department_id column
in the hcm.employees table.
select department_id, count(*) as employee_count
from hcm.employees
group by department_id
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.
select department_id, avg(salary) as average_salary
from hcm.employees
group by department_id
order by average_salary desc
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.
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
What is the date of the most recent population count at
each locality in the bird.antarctic_populations table?
select locality, max(date_of_count) as most_recent_count_date
from bird.antarctic_populations
group by locality