In this session, we will explore fundamental SQL concepts using PostgreSQL by creating and manipulating two related tables: employees and departments. The employees table will store information about employees, including their names, hire dates, salaries, and active status. The departments table will hold details about various departments within an organization. Through a series of tasks, we will practice SQL operations such as creating tables, inserting data, defining constraints, querying information, and modifying tables. This hands-on approach will enhance your understanding of SQL and its practical applications in database management using PostgreSQL.
employee_id | first_name | last_name | hire_date | salary | department_id | is_active |
---|---|---|---|---|---|---|
1 | John | Doe | 2021-05-10 | 50000.00 | 1 | TRUE |
2 | Jane | Smith | 2022-03-15 | 62000.00 | 2 | TRUE |
3 | Alice | Johnson | 2023-01-20 | 55000.00 | 1 | FALSE |
4 | Bob | Davis | 2020-11-30 | 72000.00 | 3 | TRUE |
department_id | department_name | manager_id |
---|---|---|
1 | Human Resources | 1 |
2 | Finance | 2 |
3 | Engineering | 4 |
- Create the
employees
table with various data types likeVARCHAR
,DATE
,BOOLEAN
, andNUMERIC
. - Create the
departments
table with aSERIAL
type for the primary key and aVARCHAR
type for the department name. - Ensure that the
first_name
,last_name
, anddepartment_name
columns areNOT NULL
. - Add a
PRIMARY KEY
constraint toemployee_id
anddepartment_id
. - Add a
FOREIGN KEY
constraint ondepartment_id
in theemployees
table that referencesdepartment_id
in thedepartments
table.
- Insert sample records (as shown above) into the
employees
anddepartments
tables. - Attempt to insert a record where
first_name
isNULL
to validate theNOT NULL
constraint. - Try to insert a record with a non-existent
department_id
to test theFOREIGN KEY
constraint.
-
Basic Queries:
- Select all active employees.
- Find all employees in the Finance department.
-
Aggregate Functions:
- Calculate the total salary for employees in the Engineering department.
- Determine the average salary for all employees.
-
Sorting and Aliasing:
- Select employee names and salaries, aliasing
first_name
asFirst Name
andsalary
asEmployee Salary
. - Sort employees by their hire date in descending order.
- Select employee names and salaries, aliasing
-
NULL Filtering:
- List all departments where the
manager_id
is NULL (if any records exist).
- List all departments where the
-
Using
LIKE
andBETWEEN
:- Find all employees whose
last_name
starts with "J". - Get employees hired between
2021-01-01
and2022-12-31
.
- Find all employees whose
- Use
ALTER TABLE
to add a new column,phone_number
, to theemployees
table. - Modify the
salary
column to increase its precision if necessary. - Drop the
is_active
column from theemployees
table.
- Write a query to join the
employees
anddepartments
tables to display the department name for each employee. - Find all employees and their manager's name (self-join if managers are also employees).
- Find the highest-paid employee using a subquery.
- Retrieve all employees whose salary is above the average salary.
- Create a view called
active_employees
that shows only active employees and their departments. - Select all data from the created view.
- Create an index on the
last_name
column of theemployees
table to speed up search queries. - Drop the index if needed.
- Write a query to fetch the first 2 employees ordered by
employee_id
, and then implement pagination usingLIMIT
andOFFSET
.