dbms rec (1)
dbms rec (1)
CYCLE 1
EXPERIMENT NO:4 DATE: 12/08/24
FAMILIARIZATION OF DDL COMMANDS
AIM:
Data Definition Language (DDL) - These SQL commands are used for creating, modifying,
and dropping the structure of database objects. The commands are CREATE, ALTER, DROP,
RENAME, and TRUNCATE.
QUIRES:
Consider the database for a college. Write SQL commands to implement the following:
1. Create a database
CREATE DATABASE College;
Output:
Query OK, 1 row affected
15
16
Output:
Query OK, 0 rows affected
b) Course (Course_id integer, Course_name varchar, course_duration integer)
CREATE TABLE Course(
Course_id INT,
Course_name VARCHAR(20),
course_duration INT
);
Output:
Query OK, 0 rows affected
4. List all tables in the current database.
SHOW tables;
Output:
+ +
| Tables_in_CollegeDB |
+ +
| Course |
| Student |
+ +
17
18
6. Drop the column blood_grp from Student table.
ALTER TABLE Student DROP COLUMN blood_grp;
Output:
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
7. Add a new column Adar_no with domain number to the table Student.
ALTER TABLE Student ADD Adar_no INT;
Output:
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
RESULT:
Familiarized with DDL commands of MySQL.
19
20
EXPERIMENT NO:5 DATE: 12/08/24
DATABASE MANAGEMENT FOR AN ORGANIZATION
AIM:
To implement basic SQL commands to create, modify, and manage tables within a database for
an organization.
QUIRES:
Consider the database for an organization. Write SQL commands to implement the
following:
1. Create a database
CREATE DATABASE Organization;
Output:
Query OK, 1 row affected (0.01 sec)
21
22
Output:
Query OK, 0 rows affected (0.03 sec)
23
24
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| dept_no | varchar(10) | NO | PRI | NULL | |
| dept_name | varchar(20) | YES | | NULL | |
| location | varchar(10) | YES | | NULL | |
+ + + + + + +
3 rows in set (0.00 sec)
RESULT:
The database and tables were successfully created, modified, and queried as per the
requirements.
25
26
EXPERIMENT NO:6 DATE: 12/08/24
FAMILIARIZATION OF SQL CONSTRAINTS
AIM:
To understand and implement various SQL constraints such as PRIMARY KEY, FOREIGN
KEY, NOT NULL, UNIQUE, CHECK, and to modify tables by adding and dropping
constraints.
QUIRES:
1. Create new table Persons with attributes PersonID (integer, PRIMARY KEY), Name
(varchar , NOT NULL), Aadhar (Number, NOT NULL, UNIQUE), Age (integer ,
CHECK>18).
CREATE TABLE Persons (
PersonID INTEGER PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Aadhar NUMERIC NOT NULL UNIQUE,
Age INTEGER CHECK (Age > 18)
);
Output:
Query OK, 0 rows affected
27
28
3. Display the structure of Persons tables.
DESCRIBE Persons;
Output:
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| PersonID | int | NO | PRI | NULL | |
| Name | varchar(100) | NO | | NULL | |
| Aadhar | decimal(10,0)| NO | UNI | NULL | |
| Age | int | YES | | NULL | |
+ + + + + + +
29
30
7. Add dept_no in Employee table as the foreign key reference to the table Department
with on delete cascade.
ALTER TABLE Employee ADD CONSTRAINT FK_DeptNo FOREIGN KEY
(dept_no) REFERENCES Department(dept_no) ON DELETE CASCADE;
Output:
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
RESULT:
All SQL commands were executed successfully, yielding the expected results and
demonstrating accurate data manipulation and retrieval.
31
32
EXPERIMENT NO:7 DATE: 19/08/24
FAMILIARIZATION OF DML COMMANDS
AIM:
To practice and understand Data Manipulation Language (DML) commands in SQL for
inserting, updating, deleting, and retrieving data from tables.
QUIRES:
1. Add at least 10 rows into the table Employee and Department.
INSERT INTO Department (dept_no, dept_name) VALUES
('D01', 'CS'),
('D02', 'EEE'),
('D03', 'Testing'),
('D04', 'IT'),
('D05', 'Develop');
33
34
2. Display all the records from the above tables.
SELECT *
FROM Employee;
Output:
+ + + + + + +
| emp_no | emp_name | mobile_no | dept_no | salary | designation |
+ + + + + + +
| emp1 | Alice | 1234567890 | D01 | 7000 | Analyst |
| emp2 | Bob | 1234567891 | D01 | 45000 | Developer |
| emp3 | Charlie | 1234567892 | D02 | 30000 | Developer |
| emp4 | David | 1234567893 | D03 | 35000 | Analyst |
| emp5 | Eve | 1234567894 | D02 | 38000 | Designer |
| emp6 | Frank | 1234567895 | D01 | 47000 | Developer |
| emp7 | Grace | 1234567896 | D04 | 60000 | Manager |
| emp8 | Hank | 1234567897 | D03 | 290000 | CA |
| emp9 | Ivy | 1234567898 | D05 | 123000 | Designer |
| emp10 | John | 1234567899 | D04 | 46000 | Developer |
+ + + + + + +
Output:
+ + +
| emp_no | emp_name |
+ + +
| emp3 | Charlie |
| emp5 | Eve |
+ + +
35
36
Output:
+ + + + + +
| emp_no | emp_name | designation | dept_no | salary |
+ + + + + +
| emp8 | Hank | CA | D03 | 290000 |
| emp9 | Ivy | Designer | D05 | 123000 |
| emp7 | Grace | Manager | D04 | 60000 |
| emp6 | Frank | Developer | D01 | 47000 |
| emp10 | John | Developer | D04 | 46000 |
| emp2 | Bob | Developer | D01 | 45000 |
| emp5 | Eve | Designer | D02 | 38000 |
| emp4 | David | Analyst | D03 | 35000 |
| emp3 | Charlie | Developer | D02 | 30000 |
| emp1 | Alice | Manager | D01 | 7000 |
+ + + + + +
5. Display the emp_no , name of employees whose salary is between 2000 and 5000
SELECT emp_no, emp_name
FROM Employee
WHERE salary BETWEEN 20000 AND 50000;
Output:
+ + +
| emp_no | emp_name |
+ + +
| emp2 | Bob |
| emp3 | Charlie |
| emp4 | David |
| emp5 | Eve |
| emp6 | Frank |
| emp10 | John |
+ + +
37
38
7. Change the salary of employees to 45000 whose designation is 'Manager'
UPDATE Employee
SET salary = 45000
WHERE designation = 'Manager';
Output:
Query OK, 1 rows affected
10. Retrieve the name, mobile number of all employees whose name start with “A”.
SELECT emp_no, mobile_no
FROM Employee
WHERE emp_name LIKE 'A%';
Output:
+ + +
| emp_no | mobile_no |
+ + +
| emp1 | 1234567890 |
+ + +
39
40
11. Display the details of the employee whose name has at least three characters and salary
greater than 20000.
SELECT *
FROM Employee
WHERE LENGTH(emp_name) >= 3 AND salary > 20000;
Output:
+ + + + + + +
| emp_no | emp_name | mobile_no | dept_no | salary | designation |
+ + + + + + +
| emp2 | Bob | 1234567891 | D01 | 45000 | Developer |
| emp3 | Charlie | 1234567892 | D02 | 30000 | Developer |
| emp4 | David | 1234567893 | D03 | 35000 | Analyst |
| emp5 | Eve | 1234567894 | D02 | 38000 | Designer |
| emp6 | Frank | 1234567895 | D01 | 47000 | Developer |
| emp7 | Grace | 1234567896 | D04 | 45000 | Manager |
| emp8 | Hank | 1234567897 | D03 | 290000 | CA |
| emp9 | Ivy | 1234567898 | D05 | 123000 | Designer |
| emp10 | John | 37547373 | D04 | 46000 | Developer |
+ + + + + + +
12. Display the details of employees with empid ‘emp1’, ‘emp2’ and ‘emp6’.
SELECT *
FROM Employee
WHERE emp_no = "emp1" OR emp_no = "emp2" OR emp_no = "emp6";
Output:
+ + + + + + +
| emp_no | emp_name | mobile_no | dept_no | salary | designation |
+ + + + + + +
| emp2 | Bob | 1234567891 | D01 | 45000 | Developer |
| emp6 | Frank | 1234567895 | D01 | 47000 | Developer |
+ + + + + + +
13. Display employee name and employee id of those who have salary between 120000
and 300000.
SELECT emp_name, emp_no
FROM Employee
WHERE salary BETWEEN 120000 AND 300000;
Output:
+ + +
| emp_name | emp_no |
+ + +
| Hank | emp8 |
| Ivy | emp9 |
+ + +
41
42
14. Display the details of employees whose designation is ‘Manager’ or ‘Computer
Assistant(CA)’.
SELECT *
FROM Employee
WHERE designation = "Manager" OR designation = "CA";
Output:
+ + + + + + +
| emp_no | emp_name | mobile_no | dept_no | salary | designation |
+ + + + + + +
| emp7 | Grace | 1234567896 | D04 | 45000 | Manager |
| emp8 | Hank | 1234567897 | D03 | 290000 | CA |
+ + + + + + +
43
44
17. Displays total salary of employees in each department.
SELECT dept_no, SUM(salary) AS total_salary
FROM Employee
GROUP BY dept_no;
Output:
+ + +
| dept_no | total_salary |
+ + +
| D01 | 137000 |
| D02 | 68000 |
| D03 | 325000 |
| D04 | 106000 |
| D05 | 123000 |
+ + +
18. Displays top and lower salary of employees in each department.
SELECT dept_no, MAX(salary) AS max_salary, MIN(salary) AS min_salary
FROM Employee
GROUP BY dept_no;
Output:
+ + + +
| dept_no | max_salary | min_salary |
+ + + +
| D01 | 47000 | 45000 |
| D02 | 38000 | 30000 |
| D03 | 290000 | 35000 |
| D04 | 60000 | 46000 |
| D05 | 123000 | 123000 |
+ + + +
19. Displays average salary of employees in all departments except department with
department number ‘D05’.
SELECT dept_no, AVG(salary) AS avg_salary
FROM Employee
WHERE dept_no <> 'D05'
GROUP BY dept_no;
Output:
+ + +
| dept_no | avg_salary |
+ + +
| D01 | 45666.6667 |
| D02 | 34000.0000 |
| D03 | 162500.0000 |
| D04 | 53000.0000 |
+ + +
45
46
20. Displays average salary of employees in all departments except department with
department number ‘D01’ and average salary greater than 20000 in the ascending order
of average salary.
SELECT dept_no, AVG(salary) AS avg_salary
FROM Employee
WHERE dept_no <> 'D01'
GROUP BY dept_no
HAVING AVG(salary) > 20000
ORDER BY avg_salary;
Output:
+ + +
| dept_no | avg_salary |
+ + +
| D02 | 34000.0000 |
| D04 | 53000.0000 |
| D03 | 162500.0000 |
| D05 | 123000.0000 |
+ + +
RESULT:
All SQL commands were executed successfully, yielding the expected results and
demonstrating accurate data manipulation and retrieval.
47
48
CYCLE 2
EXPERIMENT NO:8 DATE: 04/09/24
FAMILIARIZATION OF SUBQUERY, JOINS, VIEWS AND SET
OPERATIONS
AIM:
Consider the following Database Schema given at the left and execute queries to familiarize
with subqueries, joins, view and set operations.
QUIRES:
1. Create table regions.
CREATE TABLE regions
( region_id INT PRIMARY
KEY,region_name
VARCHAR(50)
);
Output:
Query OK, 0 rows affected
51
52
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
Output:
Query OK, 0 rows affected
53
54
(3, 'Asia'),
(4, 'Middle East and Africa');
Output:
Query OK, 4 rows affected
55
56
(20, 'Marketing', 1700),
(30, 'Sales', 1700),
(40, 'Finance', 1800),
(50, 'IT', 1900);
Output:
Query OK, 5 rows affected
57
58
(2, 'Michael', 'Smith', 'Son', 102),
(3, 'Emily', 'Johnson', 'Spouse', 103),
(4, 'Sophia', 'Taylor', 'Daughter', 104),
(5, 'James', 'Williams', 'Son', 105);
Output:
Query OK, 5 rows affected
59
60
+ + + +
| country_id | country_name | region_id |
+ + + +
| IT | Italy | 1 |
| US | United States | 2 |
| JP | Japan | 3 |
| IN | India | 3 |
| ZA | South Africa | 4 |
+ + + +
+ + + + +
+ +
| location_id | street_address | postal_code| city |
state_province | country_id |
+ + + + +
+ +
| 1000 | Via Roma 100 | 00100 | Rome |
Lazio | IT |
| 1700 | 2000 Broadway | 94111 | SanFrancisco|
California | US |
| 1800 | Nishi Shinjuku 3-2 | 160-0023 | Tokyo |
Tokyo | JP |
| 1900 | Block B, ITPL | 560066 | Bangalore |
Karnataka | IN |
| 2000 | 1 Mandela Way | 8001 | Cape Town |
Western Cape | ZA |
| 2100 | 123 Connaught Place | 110001 | Delhi |
Delhi | IN |
+ + + + +
+ +
+ + + +
| department_id| department_name | location_id |
+ + + +
| 10 | Administration | 1000 |
| 20 | Marketing | 1700 |
| 30 | Sales | 1700 |
| 40 | Finance | 1800 |
| 50 | IT | 1900 |
+ + + +
+ + + + +
| job_id | job_title | min_salary| max_salary|
+ + + + +
| J01 | Administrator | 10000 | 15000 |
| J02 | Marketing Specialist | 7000 | 12000 |
| J03 | Sales Manager | 12000 | 18000 |
| J04 | Finance Analyst | 10000 | 14000 |
| J05 | IT Specialist | 9000 | 13000 |
+ + + + +
61
62
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] |
1234567890 | 2020-01-15 | J01 | 12000 | | 10 |
| 102 | Jane | Smith | [email protected] |
9876543210 | 2019-03-10 | J02 | 8000 | 101 | 20 |
| 103 | Alice | Johnson | [email protected] |
5556667777 | 2018-11-25 | J03 | 15000 | 101 | 30 |
| 104 | Robert | Taylor | [email protected] |
8889990000 | 2021-07-01 | J04 | 11000 | 102 | 40 |
| 105 | Linda | Williams | [email protected] |
4445556666 | 2022-05-12 | J05 | 9500 | 103 | 50 |
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
| 109 | Zara | Ali | [email protected] |
3334445555 | 2024-02-01 | J01 | 9000 | | 10 |
| 110 | Ravi | Sharma | [email protected] |
9998887777 | 2024-03-15 | J01 | 8500 | | 20 |
| 111 | Smith | David | [email protected] |
8887776666 | 2024-04-01 | J03 | | | 40 |
+ + + + +
+ + + + + +
+ + + + + +
| dependent_id | first_name | last_name | relationship | employee_id |
+ + + + + +
| 1 | Anna | Doe | Daughter | 101 |
| 2 | Michael | Smith | Son | 102 |
| 3 | Emily | Johnson | Spouse | 103 |
| 4 | Sophia | Taylor | Daughter | 104 |
| 5 | James | Williams | Son | 105 |
+ + + + + +
16. Find all employees who locate in the location with the id 1700.
SELECT * FROM employees
WHERE department_id IN (
SELECT department_id FROM departments WHERE location_id = 1700
);
Output:
64
64
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 102 | Jane | Smith | [email protected] |
9876543210 | 2019-03-10 | J02 | 8000 | 101 | 20 |
| 103 | Alice | Johnson | [email protected] |
5556667777 | 2018-11-25 | J03 | 15000 | 101 | 30 |
| 110 | Ravi | Sharma | [email protected] |
9998887777 | 2024-03-15 | J01 | 8500 | | 20 |
+ + + + +
+ + + + + +
17. Find all employees who do not locate at the location 1700.
SELECT * FROM employees
WHERE department_id IN (
SELECT department_id FROM departments WHERE location_id <> 1700
);
Output:
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] |
1234567890 | 2020-01-15 | J01 | 12000 | NULL | 10 |
| 104 | Robert | Taylor | [email protected] |
8889990000 | 2021-07-01 | J04 | 11000 | 102 | 40 |
| 105 | Linda | Williams | [email protected] |
4445556666 | 2022-05-12 | J05 | 9500 | 103 | 50 |
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
| 109 | Zara | Ali | [email protected] |
3334445555 | 2024-02-01 | J01 | 9000 | NULL | 10 |
| 111 | Smith | David | [email protected] |
8887776666 | 2024-04-01 | J03 | NULL | NULL | NULL |
+ + + + +
+ + + + + +
65
66
18. Finds the employees who have the highest salary.
SELECT * FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Output:
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 103 | Alice | Johnson | [email protected] | 5556667777
| 2018-11-25 | J03 | 15000.00 | 101 | 30 |
+ + + + +
+ + + + + +
19. Finds all employees who salaries are greater than the average salary of all employees.
SELECT * FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Output:
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] |
1234567890 | 2020-01-15 | J01 | 12000 | NULL | 10 |
| 103 | Alice | Johnson | [email protected] |
5556667777 | 2018-11-25 | J03 | 15000 | 101 | 30 |
| 104 | Robert | Taylor | [email protected] |
8889990000 | 2021-07-01 | J04 | 11000 | 102 | 40 |
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
+ + + + +
+ + + + + +
20. Finds all departments (Department Id, Name) which have at least one employee with
the salary is greater than 10,000.
SELECT department_id, department_name
FROM departments WHERE department_id IN (
SELECT department_id FROM employees WHERE salary > 10000
);
Output:
67
68
+ + +
| department_id | department_name |
+ + +
| 10 | Administration |
| 30 | Sales |
| 40 | Finance |
| 50 | IT |
+ + +
21. Finds all departments (Department Id, Name) that do not have any employee with the
salary greater than 10,000.
SELECT department_id, department_name
FROM departments
WHERE department_id NOT IN (
SELECT department_id FROM employees WHERE salary > 10000
);
Output:
+ + +
| department_id | department_name |
+ + +
| 20 | Marketing |
+ + +
22. Finds all employees whose salaries are greater than the lowest salary of every
department.
SELECT * FROM employees
WHERE salary > (SELECT MIN(salary) FROM employees);
Output:
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] |
1234567890 | 2020-01-15 | J01 | 12000 | NULL | 10 |
| 103 | Alice | Johnson | [email protected] |
5556667777 | 2018-11-25 | J03 | 15000 | 101 | 30 |
| 104 | Robert | Taylor | [email protected] |
8889990000 | 2021-07-01 | J04 | 11000 | 102 | 40 |
| 105 | Linda | Williams | [email protected] |
4445556666 | 2022-05-12 | J05 | 9500 | 103 | 50 |
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
| 109 | Zara | Ali | [email protected] |
3334445555 | 2024-02-01 | J01 | 9000 | NULL | 10 |
| 110 | Ravi | Sharma | [email protected] |
9998887777 | 2024-03-15 | J01 | 8500 | NULL | 20 |
+ + + + +
+ + + + + +
69
70
23. Finds all employees whose salaries are greater than or equal to the highest salary of
every department.
SELECT * FROM employees
WHERE salary >= (SELECT MAX(salary) FROM employees);
Output
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 103 | Alice | Johnson | [email protected] | 5556667777
| 2018-11-25 | J03 | 15000.00 | 101 | 30 |
+ + + + +
+ + + + + +
24. Calculate the average of average salary of departments. (Hint: SQL subquery in the
FROM clause)
SELECT AVG(avg_salary)
FROM (SELECT AVG(salary) AS avg_salary FROM employees GROUP BY
department_id) AS dept_avg;
Output:
+ +
| AVG(avg_salary) |
+ +
| 11000.0000000000 |
+ +
25. Finds the salaries of all employees, their average salary, and the difference between
the salary of each employee and the average salary. (Hint: SQL Subquery in the SELECT
clause)
SELECT employee_id, salary,
(SELECT AVG(salary) FROM employees) AS avg_salary,
salary - (SELECT AVG(salary) FROM employees) AS salary_diff
FROM employees;
Output:
71
72
+ + + + +
| employee_id | salary | avg_salary | salary_diff|
+ + + + +
| 101 | 12000 | 10500 | 1500 |
| 102 | 8000 | 10500 | -2500 |
| 103 | 15000 | 10500 | 4500 |
| 104 | 11000 | 10500 | 500 |
| 105 | 9500 | 10500 | -1000 |
| 106 | 11000 | 10500 | 500 |
| 109 | 9000 | 10500 | -1500 |
| 110 | 8500 | 10500 | -2000 |
| 111 | NULL | 10500 | NULL |
+ + + + +
26. Finds all employees whose salary is higher than the average salary of the employees
in their departments. (Hint : Use Correlated Subquery).
SELECT * FROM employees AS e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id =
e.department_id);
Output:
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] |
1234567890 | 2020-01-15 | J01 | 12000 | NULL | 10 |
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
| 110 | Ravi | Sharma | [email protected] |
9998887777 | 2024-03-15 | J01 | 8500 | NULL | NULL |
+ + + + +
+ + + + + +
73
74
28. Display first name, last name, department name of employees of the Department with
id 10, 20 and 30.
SELECT first_name, last_name, department_name
FROM employees
JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_id IN (10, 20, 30);
Output:
+ + + +
| first_name | last_name | department_name |
+ + + +
| John | Doe | Administration |
| Zara | Ali | Administration |
| Jane | Smith | Marketing |
| Ravi | Sharma | Marketing |
| Alice | Johnson | Sales |
+ + + +
29. Display the first name, last name, job title, and department name of employees who
work in department with id 10, 20, and 30 and salary greater than 10000.
SELECT first_name, last_name, job_title, department_name
FROM employees
JOIN jobs ON employees.job_id = jobs.job_id
JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_id IN (10, 20, 30) AND salary > 10000;
Output:
+ + + + +
| first_name | last_name | job_title | department_name |
+ + + + +
| John | Doe | Administrator | Administration |
| Alice | Johnson | Sales Manager | Sales |
+ + + + +
30. Display Department name, street address, postal code, country name and region name
of all departments.
SELECT d.department_name, l.street_address, l.postal_code, c.country_name,
r.region_name
FROM departments d
JOIN locations l ON d.location_id = l.location_id
JOIN countries c ON l.country_id = c.country_id
JOIN regions r ON c.region_id = r.region_id;
Output:
75
76
+ + + + +-
+
| department_name | street_address | postal_code | country_name |
region_name |
+ + + + +-
+
| Administration | Via Roma 100 | 00100 | Italy |
Europe |
| Marketing | 2000 Broadway | 94111 | United States |
Americas |
| Sales | 2000 Broadway | 94111 | United States |
Americas |
| IT | Block B, ITPL | 560066 | India |
Asia |
| Finance | Nishi Shinjuku 3-2 | 160-0023 | Japan |
Asia |
+ + + + +-
+
31. Write a SQL query to find out which employees have or do not have a department.
Return first name, last name, department ID, department name.
SELECT e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;
Output:
+ + + + +
| first_name | last_name | department_id | department_name |
+ + + + +
| John | Doe | 10 | Administration |
| Jane | Smith | 20 | Marketing |
| Alice | Johnson | 30 | Sales |
| Robert | Taylor | 40 | Finance |
| Linda | Williams | 50 | IT |
| Mark | Brown | 50 | IT |
| Zara | Ali | 10 | Administration |
| Ravi | Sharma | 20 | Marketing |
| Smith | David | 40 | Finance |
+ + + + +
32. Write a SQL query to find those employees whose first name contains the letter ‘Z’.
Return first name, last name, department, city, and state province.
SELECT e.first_name, e.last_name, d.department_name, l.city, l.state_province
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
WHERE e.first_name LIKE '%Z%';
Output
77
78
+ + + + + +
| first_name | last_name | department_name | city | state_province|
+ + + + + +
| Zara | Ali | Administration | Rome | Lazio |
+ + + + + +
33. Write a SQL query to find all departments, including those without employees Return
first name, last name, department ID, department name
SELECT e.first_name, e.last_name, d.department_id, d.department_name
FROM departments d
LEFT JOIN employees e ON d.department_id = e.department_id;
Output
+ + + + +
| first_name | last_name | department_id | department_name|
+ + + + +
| John | Doe | 10 | Administration |
| Zara | Ali | 10 | Administration |
| Jane | Smith | 20 | Marketing |
| Ravi | Sharma | 20 | Marketing |
| Alice | Johnson | 30 | Sales |
| Robert | Taylor | 40 | Finance |
| Smith | David | 40 | Finance |
| Linda | Williams | 50 | IT |
| Mark | Brown | 50 | IT |
+ + + + +
34. Write a SQL query to find the employees and their managers. Those managers do not
work under any manager. Return the first name of the employee and manager.
SELECT e.first_name AS employee_name, m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
Output
+ + +
| employee_name| manager_name |
+ + +
| John | Null |
| Jane | John |
| Alice | John |
| Robert | Jane |
| Linda | Alice |
| Mark | Linda |
| Zara | Null |
| Ravi | Null |
| Smith | Null |
+ + +
79
80
35. Write a SQL query to find the employees who work in the same department as the
employee with the last name Taylor. Return first name, last name and department ID.
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'
);
Output
+ + + +
| first_name | last_name | department_id |
+ + + +
| Robert | Taylor | 40 |
+ + + +
36. Write a SQL query to calculate the difference between the maximum salary of the job
and the employee's salary. Return job title, employee name, and salary difference.
SELECT j.job_title, e.first_name || ' ' || e.last_name AS employee_name,
(j.max_salary - e.salary) AS salary_difference
FROM employees e
JOIN jobs j ON e.job_id = j.job_id;
Output
+ + + +
| job_title | employee_name | salary_difference |
+ + + +
| Administrator | John Doe | 3000 |
| Marketing Specialist | Jane Smith | 4000 |
| Sales Manager | Alice Johnson | 3000 |
| Finance Analyst | Robert Taylor | 3000 |
| IT Specialist | Linda Williams | 3500 |
| IT Specialist | Mark Brown | 2000 |
| Administrator | Zara Ali | 6000 |
| Administrator | Ravi Sharma | 6500 |
| Sales Manager | Smith David | Null |
+ + + +
37. Write a SQL query to calculate the average salary, the number of employees receiving
commissions in that department. Return department name, average salary and number
of employees of all departments.
SELECT d.department_name, AVG(e.salary) AS average_salary,
COUNT(e.employee_id) AS num_employees
FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_name;
81
82
Output
+ + + +
| department_name | average_salary | num_employees |
+ + + +
| Administration | 12000.000000 | 1 |
| Marketing | 8000.000000 | 1 |
| Sales | 15000.000000 | 1 |
| Finance | 11000.000000 | 1 |
| IT | 9500.000000 | 1 |
+ + + +
38. Create a view which contains employee name, employee id, phone number, job title,
department name, manager name of employees belongs to department whose location is
in ‘Delhi’ and display the details.
CREATE VIEW employee_details_delhi AS
SELECT e.first_name, e.last_name, e.employee_id, e.phone_number, j.job_title,
d.department_name, m.first_name AS manager_name
FROM employees e
JOIN jobs j ON e.job_id = j.job_id
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
LEFT JOIN employees m ON e.manager_id = m.employee_id
WHERE l.city = 'Delhi';
Output
Query OK, 0 rows affected
39. Use the above created view to obtain the names of employees whose job title is
‘Manager’ and department is ‘Finance’.
SELECT first_name, last_name
FROM employee_details_delhi
WHERE job_title = 'Manager' AND department_name = 'Finance';
Output
+ + +
| last_name | first_name|
+ + +
| David | Smith |
+ + +
83
84
40. Check whether it is possible to update the phone number of employee whose first
name is ‘Smith’ by using the above created view.
UPDATE employee_details_delhi
SET phone_number = '9876543210'
WHERE first_name = 'Smith';
Output
Query OK, 1 rows affected
Note: If the view allows updates, it will succeed; otherwise, an error will occur if the view is
non-updatable.
Output
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 106 | Mark | Brown | [email protected] |
9998887777 | 2024-01-10 | J05 | 11000 | 105 | 50 |
| 109 | Zara | Ali | [email protected] |
3334445555 | 2024-02-01 | J01 | 9000 | | 10 |
| 110 | Ravi | Sharma | [email protected] |
9998887777 | 2024-03-15 | J01 | 8500 | | 20 |
| 111 | Smith | David | [email protected] |
8887776666 | 2024-04-01 | J03 | | | |
+ + + + +
+ + + + + +
42. Display the details of employee who manager id is 101 or 201. (Use Union Clause)
SELECT * FROM employees WHERE manager_id = 101
UNION
SELECT * FROM employees WHERE manager_id = 201;
Output
85
86
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email | phone_number
| hire_date | job_id | salary | manager_id | department_id |
+ + + + +
+ + + + + +
| 102 | Jane | Smith | [email protected] | 9876543210
| 2019-03-10 | J02 | 8000.00 | 101 | 20 |
| 103 | Alice | Johnson | [email protected] | 5556667777
| 2018-11-25 | J03 | 15000.00 | 101 | 30 |
+ + + + +
+ + + + + +
43. Display the details of employees who have at least one dependent.
SELECT * FROM employees
WHERE employee_id IN (SELECT employee_id FROM dependents);
Output
+ + + + +
+ + + + + +
| employee_id | first_name | last_name | email |
phone_number | hire_date | job_id | salary | manager_id | department_id
|
+ + + + +
+ + + + + +
| 101 | John | Doe | [email protected] | 1234567890
| 2020-01-15 | J01 | 12000.00 | NULL | 10 |
| 102 | Jane | Smith | [email protected] | 9876543210
| 2019-03-10 | J02 | 8000.00 | 101 | 20 |
| 103 | Alice | Johnson | [email protected] | 5556667777
| 2018-11-25 | J03 | 15000.00 | 101 | 30 |
| 104 | Robert | Taylor | [email protected] | 8889990000
| 2021-07-01 | J04 | 11000.00 | 102 | 40 |
| 105 | Linda | Williams | [email protected] | 4445556666
| 2022-05-12 | J05 | 9500.00 | 103 | 50 |
+ + + + +
+ + + + + +
RESULT:
All SQL commands were executed successfully, yielding the expected results and
demonstrating accurate data creation, manipulation and retrieval.
87