0% found this document useful (0 votes)
0 views

Database Lab

The document contains SQL queries related to a database for employees, including table creation and data insertion. It includes queries to retrieve employee information based on salary, commissions, and department affiliations, as well as calculations for maximum, minimum, and average salaries. Additionally, it features queries to display employee-manager relationships and department details.

Uploaded by

Mohammad Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Database Lab

The document contains SQL queries related to a database for employees, including table creation and data insertion. It includes queries to retrieve employee information based on salary, commissions, and department affiliations, as well as calculations for maximum, minimum, and average salaries. Additionally, it features queries to display employee-manager relationships and department details.

Uploaded by

Mohammad Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Nouman

Reg No: FA21-BCS-233


Subject: Database Systems.
Titile: Mid lab.
Teacher : Sir Wajahat Ghaffar.
Date: 11/20/2024

Q1:
CREATE TABLE employees (

employee_id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

salary DECIMAL(10, 2),

commission_pct DECIMAL(5, 2),

department_id INT,

manager_id INT

);

INSERT INTO employees (employee_id, first_name, last_name, salary, commission_pct, department_id,


manager_id)

VALUES

(1, 'Ali', 'khan', 6000, 0.10, 20, 3),

(2, 'jamal', 'ali', 12000, 0.15, 50, NULL),


(3, 'Romal', 'Haqyar', 2000, NULL, 20, 1),

(4, 'Eamal', 'jawad', 3000, 0.05, 40, 1),

(5, 'Fawad', 'khan', 5000, NULL, 80, 2);

Q1:
1. list the last name and salary of employees who earn between $5,000 and $12,000, and are
in department 20 or 50. Label the columns Employee and Monthly Salary, respectively.

SELECT last_name AS Employee, salary AS "Monthly Salary"

FROM employees

WHERE salary BETWEEN 5000 AND 12000

AND department_id IN (20, 50);


Q2: Display the last name, salary, and commission for all employees who earn commissions.
Sort data in descending order of salary and commissions.

SELECT last_name, salary, commission_pct

FROM employees

WHERE commission_pct IS NOT NULL

ORDER BY salary DESC, commission_pct DESC;

Q3: Display the maximin, minimum and average salary given to the employees in our
organization.

SELECT

MAX(salary) AS "Maximum Salary",

MIN(salary) AS "Minimum Salary",

AVG(salary) AS "Average Salary"

FROM employees;

Q4: Display the name of the employee along with their total annual salary (Salary+commision).
The name of the employee earning highest annual salary should appear first.
SELECT last_name, (salary + COALESCE(commission_pct, 0) * salary) AS "Total Annual Salary"

FROM employees

ORDER BY "Total Annual Salary" DESC;

Q5: Write a query to display first name, last name, department name for all employees working
in department 80 or 40.

Manager Table:

SELECT e.first_name, e.last_name, d.department_name


FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
WHERE e.department_id IN (40, 80);

Q6: Write a query in SQL to display the department name, city, and state province for each
department.

SELECT d.department_name, l.city, l.state_province


FROM departments d
INNER JOIN locations l ON d.location_id = l.location_id;

Q7: Write a query in SQL to display the first name, last name, and department number for those
employees who works in the same department as the employee who holds the last name as
Taylor.

SELECT e.first_name, e.last_name, e.department_id


FROM employees e
WHERE e.department_id = (
SELECT department_id
FROM employees
WHERE last_name = 'Taylor'
LIMIT 1
);

8Q: Write a query in SQL to display the first name of all employees and the first name of their
manager including those who does not working under any manager.

SELECT e.first_name AS "Employee",


COALESCE(m.first_name, 'No Manager') AS "Manager"
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;

You might also like