Exercises - Mastering Postgresql - Mastering SQL Using Postgresql
Exercises - Mastering Postgresql - Mastering SQL Using Postgresql
Getting Started
Here is the exercise related to getting started module.
You can connect to the database using following details in the environment provided by us.
Host: localhost
Port: 5342
Database Name: YOUR_OS_USER_hr_db
User Name: YOUR_OS_USER_hr_user
Password: YOUR_OS_USER_PASSWORD (provided by us).
If you are using your own environment, make sure to create database for storing HR Data.
Database Name: hr_db
User Name: hr_user
You can create user with password of your choice.
Understand data.
Check for delimiters (record as well as field).
Check whether header exists or not.
Ensure number of fields for the table and data being loaded are same or not.
Load data into the table using COPY Command.
Validate by running these queries. You can also use SQL Workbench to run the queries to validate whether
data is loaded successfully or not.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 1/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
https://postgresql.itversity.com/mastering_postgresql_exercises.html 2/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Validation - Get count of all published courses by author and make sure output is sorted in descending order by
count.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 3/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Bob Dillon 5
Elvis Presley 2
Uncle Sam 1
Ensure that we have required database and user for retail data. We might provide the database as part of
our labs. Here are the instructions to use psql for setting up the required database (if required) and tables.
Create Tables using the script provided. You can either use psql or SQL Workbench.
psql -U itversity_retail_user \
-h localhost \
-p 5432 \
-d itversity_retail_db \
-W
Once the tables are dropped you can run below script to create the tables for the purpose of exercises.
\i /data/retail_db/create_db_tables_pg.sql
\i /data/retail_db/load_db_tables_pg.sql
https://postgresql.itversity.com/mastering_postgresql_exercises.html 4/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Here are the high level steps for database migration from one type of database to another type of database.
Extract DDL Statements from source database (MySQL).
Extract the data in the form of delimited files and ship them to target database.
Refactor scripts as per target database (Postgres).
Create tables in the target database.
Execute pre-migration steps (disable constraints, drop indexes etc).
Load the data using native utilities.
Execute post-migration steps (enable constraints, create or rebuild indexes, reset sequences etc).
Sanity checks with basic queries.
Make sure all the impacted applications are validated thoroughly.
We have scripts and data set available in our GitHub repository. If you are using our environment the
repository is already cloned under /data/retail_db.
It have scripts to create tables with primary keys. Those scripts are generated from MySQL tables and
refactored for Postgres.
Script to create tables: create_db_tables_pg.sql
Load data into tables: load_db_tables_pg.sql
Here are the steps you need to perform to take care of this exercise.
Create tables
Load data
All the tables have surrogate primary keys. Here are the details.
orders.order_id
order_items.order_item_id
customers.customer_id
products.product_id
https://postgresql.itversity.com/mastering_postgresql_exercises.html 5/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
categories.category_id
departments.department_id
Get the maximum value from all surrogate primary key fields.
Create sequences for all surrogate primary key fields using maximum value. Make sure to use
standard naming conventions for sequences.
Ensure sequences are mapped to the surrogate primary key fields.
Create foreign key constraints based up on this information.
orders.order_customer_id to customers.customer_id
order_items.order_item_order_id to orders.order_id
order_items.order_item_product_id to products.product_id
products.product_category_id to categories.category_id
categories.category_department_id to departments.department_id
Insert few records in departments to ensure that sequence generated numbers are used for
department_id.
Here are the commands to launch psql and run scripts to create tables as well as load data into tables.
psql -U itversity_retail_user \
-h localhost \
-p 5432 \
-d itversity_retail_db \
-W
\i /data/retail_db/create_db_tables_pg.sql
\i /data/retail_db/load_db_tables_pg.sql
We use this approach of creating tables, loading data and then adding constraints as well as resetting
sequences for large volume data migrations from one database to another database.
Here are the commands or queries you need to come up with to solve this problem.
Exercise 1
Queries to get maximum values from surrogate primary keys.
Exercise 2
Commands to add sequences with START WITH pointing to the maximum value for the corresponding surrogate
primary key fields. Make sure to use meaningful names to sequences TABLENAME_SURROGATEFIELD_seq
(example: users_user_id_seq for users.user_id)
Exercise 3
Commands to alter sequences to bind them to corresponding surrogate primary key fields.
Exercise 4
Add Foreign Key constraints to the tables.
Validate if the tables have data violataing foreign key constraints (Hint: You can use left outer join to find
rows in child table but not in parent table)
Alter tables to add foreign keys as specified.
Here are the relationships for your reference.
orders.order_customer_id to customers.customer_id
order_items.order_item_order_id to orders.order_id
order_items.order_item_product_id to products.product_id
Solution should contain the following:
Commands to add foreign keys to the tables.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 6/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Exercise 5
Queries to validate whether constraints are created or not. You can come up with queries against
information_schema tables such as columns, sequences etc.
Partitioning Tables
Here is the exercise to get comfort with partitioning. We will be using range partitioning.
Exercise 1
Create table orders_part with the same columns as orders.
Exercise 2
Let us load and validate data in the partitioned table.
Pre-Defined Functions
Here are the exercises to ensure our understanding related to Pre-Defined Functions.
We will use users table as well as other tables we got as part of retail database.
Information will be provided with each exercise.
%load_ext sql
%env
DATABASE_URL=postgresql://itversity_retail_user:retail_password@localh
ost:5432/itversity_retail_db
%%sql
https://postgresql.itversity.com/mastering_postgresql_exercises.html 7/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
%%sql
%%sql
https://postgresql.itversity.com/mastering_postgresql_exercises.html 8/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Exercise 1
Get all the number of users created per year.
created_year user_count
2018 13
2019 4
2020 8
Exercise 2
Get the day name of the birth days for all the users born in the month of June.
Exercise 3
Get the names and email ids of users added in year 2019.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 9/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Exercise 4
Get the number of users by gender.
user_gender user_count
Female 13
Male 10
Not Specified 2
Exercise 5
Get last 4 digits of unique ids.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 10/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
1 88833-8759 8759
2 262501-029 1029
3 391-33-2823 2823
4 1195413-80 1380
5 471-24-6869 6869
6 192374-933 4933
7 749-27-47-52 4752
8 461-75-4198 4198
9 892-36-676-2 6762
10 197-54-1646 1646
11 232-55-52-58 5258
12 898-84-336-6 3366
13 247-95-68-44 6844
14 415-48-894-3 8943
15 403-39-5-869 5869
16 399-83-05-03 0503
17 607-99-0411 0411
18 430-01-578-5 5785
19 571-09-6181 6181
20 478-32-02-87 0287
21 Not Specified
22 Not Specified
https://postgresql.itversity.com/mastering_postgresql_exercises.html 11/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
23 830-40-5287 5287
25 368-44-4478 4478
Exercise 6
Get the count of users based up on country code.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 12/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
country_code user_count
1 1
7 2
48 1
54 1
55 1
62 3
63 1
81 1
84 1
86 4
229 1
249 1
351 1
370 1
380 1
420 1
598 1
Exercise 7
Let us validate if we have invalid order_item_subtotal as part of order_items table.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 13/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
count
Exercise 8
Get number of orders placed on weekdays and weekends in the month of January 2014.
day_type order_count
Analytics Functions
Let us take care of the exercises related to analytics functions. We will be using HR database for the same.
Get all the employees who is making more than average salary with in each department.
Get cumulative salary for one of the department along with department name.
Get top 3 paid employees with in each department by salary (use dense_rank)
Get top 3 products sold in the month of 2014 January by revenue.
Get top 3 products in each category sold in the month of 2014 January by revenue.
Prepare HR Database
Here are the steps to prepare HR database.
Connect to HR DB using psql or SQL Workbench. Here is the sample psql command.
psql -h localhost \
-p 5432 \
-d itversity_hr_db \
-U itversity_hr_user \
-W
Run scripts to create tables and load the data. You can also drop the tables if they already exists.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 14/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
\i /data/hr_db/drop_tables_pg.sql
\i /data/hr_db/create_tables_pg.sql
\i /data/hr_db/load_data_pg.sql
Validate to ensure that data is available in the tables by running these queries.
%load_ext sql
%env
DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432
/itversity_hr_db
env:
DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:54
32/itversity_hr_db
* postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db
10 rows affected.
19
100 Steven King SKING 515.123.4567
19
101 Neena Kochhar NKOCHHAR 515.123.4568
19
102 Lex De Haan LDEHAAN 515.123.4569
19
103 Alexander Hunold AHUNOLD 590.423.4567
19
104 Bruce Ernst BERNST 590.423.4568
19
105 David Austin DAUSTIN 590.423.4569
19
106 Valli Pataballa VPATABAL 590.423.4560
19
107 Diana Lorentz DLORENTZ 590.423.5567
19
108 Nancy Greenberg NGREENBE 515.124.4569
19
109 Daniel Faviet DFAVIET 515.124.4169
%%sql
* postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db
10 rows affected.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 15/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
60 IT 103 1400
Ensure that we have required database and user for retail data. We might provide the database as part of
our labs. Here are the instructions to use psql for setting up the required database (if required) and tables.
Create Tables using the script provided. You can either use psql or SQL Workbench.
psql -U itversity_retail_user \
-h localhost \
-p 5432 \
-d itversity_retail_db \
-W
Once the tables are dropped you can run below script to create the tables for the purpose of exercises.
\i /data/retail_db/create_db_tables_pg.sql
\i /data/retail_db/load_db_tables_pg.sql
%load_ext sql
%env
DATABASE_URL=postgresql://itversity_retail_user:retail_password@localh
ost:5432/itversity_retail_db
https://postgresql.itversity.com/mastering_postgresql_exercises.html 16/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
env:
DATABASE_URL=postgresql://itversity_retail_user:retail_password@loca
lhost:5432/itversity_retail_db
10 rows affected.
2013-07-25
1 11599 CLOSED
00:00:00
2013-07-25
2 256 PENDING_PAYMENT
00:00:00
2013-07-25
3 12111 COMPLETE
00:00:00
2013-07-25
4 8827 CLOSED
00:00:00
2013-07-25
5 11318 COMPLETE
00:00:00
2013-07-25
6 7130 COMPLETE
00:00:00
2013-07-25
7 4530 COMPLETE
00:00:00
2013-07-25
8 2911 PROCESSING
00:00:00
2013-07-25
9 5657 PENDING_PAYMENT
00:00:00
2013-07-25
10 5648 PENDING_PAYMENT
00:00:00
*
postgresql://itversity_retail_user:***@localhost:5432/itversity_reta
il_db
1 rows affected.
count
68883
*
postgresql://itversity_retail_user:***@localhost:5432/itversity_reta
il_db
10 rows affected.
1 1 957
2 2 1073
3 2 502
4 2 403
5 4 897
6 4 365
7 4 502
8 4 1014
9 5 957
10 5 365
https://postgresql.itversity.com/mastering_postgresql_exercises.html 17/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
*
postgresql://itversity_retail_user:***@localhost:5432/itversity_reta
il_db
1 rows affected.
count
172198
*
postgresql://itversity_retail_user:***@localhost:5432/itversity_reta
il_db
10 rows affected.
Quest Q64 10
FT. x 10 FT.
1 2
Slant Leg
Instant U
Under Armour
Men's
2 2
Highlight MC
Football Clea
Under Armour
Men's
3 2 Renegade D
Mid Football
Cl
Under Armour
Men's
4 2 Renegade D
Mid Football
Cl
Riddell Youth
Revolution
5 2
Speed Custom
Footbal
Jordan Men's
6 2 VI Retro TD
Football Cleat
Schutt Youth
Recruit
7 2 Hybrid
Custom
Football H
Nike Men's
Vapor Carbon
8 2
Elite TD
Football Cle
Nike Adult
Vapor Jet 3.0
9 2
Receiver
Gloves
Under Armour
Men's
10 2
Highlight MC
Football Clea
*
postgresql://itversity_retail_user:***@localhost:5432/itversity_reta
il_db
1 rows affected.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 18/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
count
1345
Exercise 1
Get all the employees who is making more than average salary with in each department.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 19/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
https://postgresql.itversity.com/mastering_postgresql_exercises.html 20/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
%load_ext sql
%env
DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432
/itversity_hr_db
env:
DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:54
32/itversity_hr_db
Exercise 2
Get cumulative salary with in each department for Finance and IT department along with department name.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 21/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Compute cumulative salary expense for Finance as well as IT departments with in respective departments.
Make sure cumulative salary expense per department is rounded off to 2 decimals.
Output should contain employee_id, department_name, salary and cum_salary_expense (derived field).
Data should be sorted in ascending order by department_name and then salary.
Exercise 3
Get top 3 paid employees with in each department by salary (use dense_rank)
https://postgresql.itversity.com/mastering_postgresql_exercises.html 22/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
103 60 IT 9000.00 1
104 60 IT 6000.00 2
105 60 IT 4800.00 3
106 60 IT 4800.00 3
https://postgresql.itversity.com/mastering_postgresql_exercises.html 23/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
Exercise 4
Get top 3 products sold in the month of 2014 January by revenue.
Exercise 5
Get top 3 products sold in the month of 2014 January under selected categories by revenue. The categories are
Cardio Equipment and Strength Training.
Use retail database tables such as orders, order_items, products as well as categories.
Highest revenue generating product should come at top.
Consider only those orders which are either in COMPLETE or CLOSED status.
Output should contain category_id, category_name, product_id, product_name, revenue, product_rank.
revenue and product_rank are derived fields.
Data should be sorted in ascending order by category_id and descending order by revenue.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 24/25
26/10/2024, 18:53 Exercises - Mastering Postgresql — Mastering SQL using Postgresql
By Durga Gadiraju
© Copyright ITVersity, Inc.
https://postgresql.itversity.com/mastering_postgresql_exercises.html 25/25