Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
112 views

DBMS Lab-I - Questions

The document provides examples of SQL queries to implement in a database management systems lab. It includes 10 queries in the first set focusing on aggregating employee data by job, manager, department, and salary. The second set provides 10 boat reservation queries involving sailors, boats, and reservations. The third set has 3 additional employee queries involving filtering by job, salary comparisons, and employee details.

Uploaded by

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

DBMS Lab-I - Questions

The document provides examples of SQL queries to implement in a database management systems lab. It includes 10 queries in the first set focusing on aggregating employee data by job, manager, department, and salary. The second set provides 10 boat reservation queries involving sailors, boats, and reservations. The third set has 3 additional employee queries involving filtering by job, salary comparisons, and employee details.

Uploaded by

Sohan Gattu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ICFAI Foundation for Higher Education, Faculty of Science & Technology

Fourth Semester 2022-23


Database Management Systems (CS206)
LAB-1 Questions
Date of Examination: 10/10/2023 To 13/10/2023 in the Respective labs
_________________________________________________________________________

SET-1

write the following queries, by implementing the necessary tables.

1. Display the total salary spent for each job category.

mysql> select job,sum(sal) from emp group by job;


+-----------+----------+
| job | sum(sal) |
+-----------+----------+
| ANALYST | 3000.00 |
| CLERK | 4150.00 |
| MANAGER | 8275.00 |
| PRESIDENT | 5000.00 |
| SALESMAN | 5600.00 |
+-----------+----------+
5 rows in set (0.00 sec)

2. Display the lowest paid employee details under each manager.

mysql> SELECT mgr, MIN(sal)


-> from emp group by mgr order by min(sal);
+------+----------+
| mgr | MIN(sal) |
+------+----------+
| 7902 | 800.00 |
| 7698 | 950.00 |
| 7788 | 1100.00 |
| 7782 | 1300.00 |
| 7839 | 2450.00 |
| 7566 | 3000.00 |
| NULL | 5000.00 |
+------+----------+
7 rows in set (0.13 sec)
3. Display the number of employees working in each department and their department no.

mysql> select deptno,count(*) from emp group by deptno;


+--------+----------+
| deptno | count(*) |
+--------+----------+
| 10 | 3|
| 20 | 4|
| 30 | 6|
+--------+----------+
3 rows in set (0.03 sec)

4. Display the details of employees sorting the salary in increasing order.

mysql> select * from emp order by sal;


+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100.00 | NULL | 20 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
13 rows in set (0.00 sec)

5. Show the record of employees earning salaries greater than 1600 in each department.

mysql> select * from emp where sal>1600;


+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+-----------+------+------------+---------+------+--------+
5 rows in set (0.01 sec)

6. Display the lowest paid and highest paid employee details under each department.

mysql> select deptno,min(sal),max(sal) from emp group by deptno;


+--------+----------+----------+
| deptno | min(sal) | max(sal) |
+--------+----------+----------+
| 10 | 1300.00 | 5000.00 |
| 20 | 800.00 | 3000.00 |
| 30 | 950.00 | 2850.00 |
+--------+----------+----------+
3 rows in set (0.02 sec)

7. Display the second-highest salary employee details.

mysql> select * from emp order by sal limit 1,1;


+-------+-------+-------+------+------------+--------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-------+------+------------+--------+------+--------+
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
+-------+-------+-------+------+------------+--------+------+--------+
1 row in set (0.00 sec)

8. Display the department names which are having more than 5 employees.

mysql> select deptno,count(*) from emp group by deptno having count(*)>5;


+--------+----------+
| deptno | count(*) |
+--------+----------+
| 30 | 6|
+--------+----------+
1 row in set (0.00 sec)

With subquery want to display the department name


mysql> select dname from dept where deptno IN (select deptno from emp group by deptno having
count(*)>5);
+-------+
| dname |
+-------+
| SALES |
+-------+
1 row in set (0.00 sec)
9. Display the employee details who is getting the 5th highest salary.

mysql> select * from emp order by sal limit 4,1;


+-------+--------+----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+---------+---------+--------+
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
+-------+--------+----------+------+------------+---------+---------+--------+
1 row in set (0.00 sec)

10. Display the department names whose salary budget crossed 1lakh.

mysql> select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;


+--------+----------+
| deptno | sum(sal) |
+--------+----------+
| 30 | 9400.00 |
+--------+----------+
1 row in set (0.00 sec)

With subquery want to display the department name

mysql> select dname from dept where deptno IN (select deptno from emp group by deptno having
sum(sal)>9000);
+-------+
| dname |
+-------+
| SALES |
+-------+
1 row in set (0.17 sec)

SET-2

write the following queries, by implementing the necessary tables.

1. Find all information about the sailors who have reserved boat number 101.

mysql> SELECT S.*


-> FROM Sailors S, Reserves R
-> WHERE S.sid = R.sid AND R.bid = 103;
+-----+---------+--------+------+
| sid | sname | rating | age |
+-----+---------+--------+------+
| 22 | Dustin | 7 | 45 |
| 31 | Lubber | 8 | 55.5 |
| 74 | Horatio | 9 | 40 |
+-----+---------+--------+------+
3 rows in set (0.08 sec)

2. Find the name of the boat reserved by Bob.

mysql> SELECT bname


-> FROM sailors s, boats b, reserves r
-> WHERE r.sid=s.sid AND r.bid=b.bid AND
-> sname='Bob';
Empty set (0.02 sec)

3. Find the names of sailors who have reserved a red boat, and list them in the order of age.

mysql> SELECT S.sname, S.age


-> FROM Sailors S, Reserves R, Boats B
-> WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = 'red'
-> ORDER BY S.age;
+---------+------+
| sname | age |
+---------+------+
| Horatio | 35 |
| Dustin | 45 |
| Dustin | 45 |
| Lubber | 55.5 |
| Lubber | 55.5 |
+---------+------+
5 rows in set (0.00 sec)

4. Find the names of sailors who have reserved at least one boat.

mysql> SELECT DISTINCT sname


-> FROM Sailors S, Reserves R
-> WHERE S.sid = R.sid;
+---------+
| sname |
+---------+
| Dustin |
| Horatio |
| Lubber |
+---------+
3 rows in set (0.00 sec)
5. Find the ids and names of sailors who have reserved two different boats on the same day.

mysql> SELECT DISTINCT S.sid, S.sname


-> FROM Sailors S, Reserves R1, Reserves R2
-> WHERE S.sid = R1.sid AND S.sid = R2.sid
-> AND R1.day = R2.day AND R1.bid <> R2.bid;
+-----+--------+
| sid | sname |
+-----+--------+
| 22 | Dustin |
+-----+--------+
1 row in set (0.02 sec)

6. Find the ids of sailors who have reserved a red or green boat.
mysql> SELECT R.sid
-> FROM Boats B, Reserves R
-> WHERE R.bid = B.bid AND B.color = 'red'
-> UNION
-> SELECT R2.sid
-> FROM Boats B2, Reserves R2
-> WHERE R2.bid = B2.bid AND B2.color = 'green';
+-----+
| sid |
+-----+
| 22 |
| 31 |
| 64 |
| 74 |
+-----+
4 rows in set (0.16 sec)

7. Find the name and the age of the youngest sailor.

mysql> SELECT S.sname, S.age


-> FROM Sailors S
-> WHERE S.age <= ALL ( SELECT age
-> FROM Sailors ) ;
+-------+------+
| sname | age |
+-------+------+
| Zorba | 16 |
+-------+------+
1 row in set (0.02 sec)

8. Count the number of different sailor names.


9. Find the average age of sailors for each rating level.

mysql> SELECT rating, AVG(age)


-> FROM sailors
-> GROUP BY rating ;
+--------+----------+
| rating | AVG(age) |
+--------+----------+
| 7| 40 |
| 1| 33 |
| 8 | 40.5 |
| 10 | 25.5 |
| 9| 40 |
| 3 | 44.5 |
| NULL | 48 |
+--------+----------+
7 rows in set (0.02 sec)

10. Find the average sailor’s age for each rating level with at least two sailors.

mysql> SELECT S.rating, AVG(S.age) AS avgage

-> FROM Sailors S

-> GROUP BY S.rating

-> HAVING COUNT(*) > 1 ;

+--------+--------+

| rating | avgage |

+--------+--------+

| 7| 40 |

| 8 | 40.5 |

| 10 | 25.5 |

| 3 | 44.5 |

+--------+--------+

4 rows in set (0.02 sec)


SET-3

write the following queries, by implementing the necessary tables.

1. List the E_no, E_name, and Salary of all employees working for MANAGER.

2. Display all the details of the employee whose salary is more than the Sal of any CLERK

mysql> select * from emp where sal> any( select sal from emp where job='CLERK');
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
12 rows in set (0.01 sec)
.

3. List the employees in the ascending order of Designations of those joined after 1981.

mysql> SELECT *
-> FROM emp
-> WHERE hiredate>('1981-01-01') order by job;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
+-------+--------+-----------+------+------------+---------+---------+--------+
12 rows in set (0.01 sec)

4. List the employees along with their Experience and Daily Salary.

5. List the employees who are either ‘CLERK’ or ‘ANALYST’.

6. List the employees who joined on 1-MAY-81, 3-DEC-81, 17-DEC-81,19-JAN-80.

7. List the employees who are working for the Deptno 10 or20.

8. List the Enames that are starting with ‘S’ .

mysql> SELECT *
-> FROM emp
-> where ename like 's%';
+-------+-------+-------+------+------------+--------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-------+------+------------+--------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
+-------+-------+-------+------+------------+--------+------+--------+
1 row in set (0.02 sec)

9. Display the name as well as the first five characters of name(s) starting with ‘H’

10. List all the emps except ‘PRESIDENT’ & ‘MANAGER” in asc order of Salaries.
SET-4

Create a table called EMP with the following structure.


Name Type

EMPNO NUMBER (6)


ENAME VARCHAR2 (20)
JOB VARCHAR2 (10)
DEPTNO NUMBER (3)
SAL NUMBER (7,2)

1. Allow NULL for all columns except ename and job. (Either in Create command or Alter Command)
2. Add constraints to check, while entering the empno value (i.e) empno > 1000.

Alter table Employee add constraint ck_emp check(empno>1000);

3. Define the field DEPTNO as unique.


create table dept (deptno int unique,deptname char(10));

4. Create a primary key constraint for the table(EMPNO).

create table emp(empno int primary key, ename char(10));

5. Create a default constraint and provide a salary value of "1000" if the salary is not provided.

CREATE TABLE employee (id INT ,name varchar(255) NOT NULL,


salary DECIMAL (20, 1) DEFAULT 10000.0)

6. Add the hire_date, bdate columns to the existing table and insert the values in the column.
7. Display the most experienced employee details.
8. Display the employee details who was born on august 15th.
9. Display the employee details as follows
CLARK working as MANAGER in the ACCOUNTING department
SMITH working as CLERK in the RESEARCH department

10. Display all the employees and the departments implementing a left outer join.

SELECT emp.empid, emp.FirstName, dept.DeptId, dept.Name


FROM Employee emp
LEFT JOIN Department dept
ON emp.DeptId = dept.DeptId;
SET-5

Create a table EMPLOYEE with following schema:

(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)

Write SQL queries for following question:

1. Display all the information in the EMP table.

SELECT * from emp;

2. change the table name from EMP to EMP_ORIGINAL

rename table emp to emp_original;

3. Display the record of each employee who works in department 10.

SELECT* from employee WHERE department=10;

4. Update the city of Emp_no-1234 with the current city as Hyderabad.

update employee set city=”mumbai” WHERE emp_no=1234;

5. Display the Employee Id and Employee Name who works in the department of Marketing.

SELECT first_name, last_name, department_id FROM employees WHERE


department_id = (SELECT department_id FROM departments WHERE
department_name = 'Marketing');

6. Delete the information of employee James.

delete from employee where ename=”james”;

7. Display the complete record of employees working in the SALES Department.

SELECT * FROM employees WHERE department_id = (SELECT


department_id FROM departments WHERE department_name = 'sales');
8. Create a dummy table Emp and copy the records of the EMPLOYEE table.

CREATE TABLE employees_dummy SELECT * FROM employees;

9. Create another dummy table Emp_Dum and copy only specific columns (Emp_no, E_name,Job_id) of
EMPLOYEE table.

CREATE TABLE emp_dum SELECT Emp_no,E_name,job_id FROM employees;

10. List all Employee names, salary and 15% rise in salary. Label the column as a pay hike.

select 1.15*sal as payhike from Employee;


+-----------+
| payhike |
+-----------+
| 382.95000 |
+-----------+

You might also like