SQL Lab Record1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SQL LAB RECORD-1

21.03.2024
mysql> create database ann2025;
Query OK, 1 row affected (0.00 sec)
mysql> use ann2025;
Database changed
mysql> select * from dept;
deptn dname loc
o
10 admin Chennai
20 HR EKM
30 SALES CHENNAI
40 PURCHASE TKM
50 IT EKM
5 rows in set (0.01 sec)
mysql> select *from emp;
empid job mgr hiredate sal comm deptno
ename
7369 Smith Clerk 7902 2018-12-17 80000.00 1000 10
7498 allen salesman 7698 2018-02-20 16000.00 300 30
7499 allen salesman 7698 2020-02-20 16000.00 3000 30
7521 ward salesman 7698 2019-02-22 12500.00 500 30
7566 joes manager 7839 2020-04-02 55750.00 1000 20
7654 salesman 7698 2020-09-28 29750.00 1000 40
martin
7698 blake clerk 1980-01-03 4545.00 NULL 20
NULL
7782 clark manager 7839 1980-05-09 55750.00 3000 10
8 rows in set (0.00 sec)

SQL Query
1. Display unique jobs from EMP table.
mysql> select distinct job from emp;

job

ANNMARYKURUVILLA XIIA

C1-3 1
Clerk
salesman
manager
3 rows in set (0.00 sec)

2. List the employees in the asc order of their Salaries.


mysql> select ename from emp order by sal;

ename
blake
ward
allen
allen
martin
joes
clark
Smith
8 rows in set (0.02 sec)

3. Display all the unique job groups in the descending order.

mysql> select distinct job from emp order by job desc;


job
salesman
manager
Clerk
3 rows in set (0.00 sec)

4. List the employees who joined before 1981.


mysql> select ename from emp where year(hiredate)<1981;
ename
blake
clark
2 rows in set (0.01 sec)

5. List the Empno, ename, salary, daily sal of all emps in the asc order of job (sal is given
monthly).
mysql> select empid, ename, sal, 30/sal from emp order by job;
ename sal 30/sal
empid

ANNMARYKURUVILLA XIIA

C1-3 2
7369 Smith 80000.00 0.000375
7698 blake 4545.00 0.006601
7566 joes 55750.00 0.000538
7782 clark 55750.00 0.000538
7498 allen 16000.00 0.001875
7499 allen 16000.00 0.001875
7521 ward 12500.00 0.002400
7654 martin 29750.00 0.001008
8 rows in set (0.00 sec)

6. List the empno, ename and department name of all the employees whose manager id is
not entered.
mysql> select empid, ename, job,dept from emp,department where dept.deptno=emp.deptno
and mgr is null;
empid ename job Dname
7698 blake clerk HR
1 row in set (0.01 sec)

7. Display all the details of the emp name, salary, comm and department no, name and
location where the location is in Chennai.
mysql> select ename, sal, comm, dept.deptno, dname, loc from dept, emp where
dept.deptno=emp.deptno and loc in ('chennai');
sal comm deptno dname loc
ename
Smith 80000.00 1000 10 admin Chennai
allen 16000.00 300 30 SALES CHENNAI
allen 16000.00 3000 30 SALES CHENNAI
ward 12500.00 500 30 SALES CHENNAI
clark 55750.00 3000 10 admin Chennai
5 rows in set (0.00 sec)

8. List the employee along with their Daily salary where the monthly sal is more than
Rs.10000 (sal is given monthly).
mysql> select ename, sal/30 dailysal from emp where sal>10000;
ename dailysal
Smith 2666.666667
allen 533.333333
allen 533.333333
ward 416.666667
joes 1858.333333
martin 991.666667
ANNMARYKURUVILLA XIIA

C1-3 3
clark 1858.333333
7 rows in set (0.00 sec)

9. List the employee details who are either ‘Clerk’ or ‘Analyst’ in the descending order of
job.
mysql> select ename from emp where job in ('Clerk', 'Analyst') order by job desc;

ename
Smith
blake
2 rows in set (0.00 sec)

10. List the employee’s details who are working for the dept purchase and hr.
mysql> select * from emp,dept where dept.deptno=emp.deptno and dname
in('purchase','hr');
empid ename job mgr hiredate sal comm deptno deptno dname loc
7566 joes manager 7839 2020-04- 55750.00 1000 20 20 HR EKM
02
7698 2020-09- 29750.00 1000 40 40
7654 martin salesma 28 PURCHAS TKM
n E
7698 blake clerk NULL 1980-01- 4545.00 NULL 20 20 HR EKM
03

11. List the employee name and horedate who are joined before 1980.
mysql> select ename, hiredate from emp where year(hiredate)<1980;
Empty set (0.00 sec)

12. List the employees name and department name who are joined in the month of August.
mysql> select ename, dname from dept, emp where dept.deptno=emp.deptno and
monthname(hiredate)='August';
Empty set (0.00 sec)

13. List the employees name, salary and annual salary whose annual sal ranging from
95000 and 198000.
mysql> select ename,sal,sal*12 annualsal from emp where sal*12 between 95000 and
198000;

ename sal annualsal


allen 192000.00
16000.00
ANNMARYKURUVILLA XIIA

C1-3 4
allen 192000.00
16000.00
ward 150000.00
12500.00
3 rows in set (0.00 sec)

14. List the employee names those having five characters in their names.
mysql> select ename from emp where length(ename)=5;
ename
Smith
allen
allen
blake
clark
5 rows in set (0.00 sec)

15. List the Employee names those starting with ‘S’ and with five characters.
mysql> select ename from emp where ename like ('s____');
ename
Smith
1 row in set (0.00 sec)

16. List the five characters names starting with ‘s’ and ending with ‘h’.
mysql> select ename from emp where ename like ('s___h');
ename
Smith
1 row in set (0.02 sec)

17. Find the number of employees under each department.


mysql> select count(emp.deptno),job, dname from dept,emp where dept.deptno=emp.deptno
group by dname;

count(emp.deptno) job dname


2 Clerk admin
2 manager HR
1 salesman PURCHASE
3 salesman SALES
4 rows in set (0.00 sec)

ANNMARYKURUVILLA XIIA

C1-3 5
18. List the employees whose salary is between 16000 and 70000
mysql> select ename from emp where sal between 16000 and 70000;
ename
allen
allen
joes
martin
clark
5 rows in set (0.00 sec)

19. Display the highest and the lowest salary of the employees who has joined in the same
year.
mysql> select max(sal),min(sal) from emp group by year(hiredate);
max(sal) min(sal)
55750.00 4545.00
80000.00 16000.00
12500.00 12500.00
55750.00 16000.00
4 rows in set (0.00 sec)

20. Find the employee salary, comm and name who are not having comm
mysql> select sal, comm, ename from emp where comm is null;
sal comm ename
4545.00 blake
NULL
1 row in set (0.01 sec)
21. Display the no. of people who are earning the same salary where the total employee is
more than 1.
mysql> select count(*), sal from emp group by sal having count(*) >1;

count(* sal
)
2 16000.00
2 55750.00
2 rows in set (0.00 sec)

22. Display the no. of employee, and the total salary who is working in the same
designation.
mysql> select count(*) no_emp, sum(sal),job from emp group by job;
ANNMARYKURUVILLA XIIA

C1-3 6
no_em sum(sal) job
p
2 84545.00 Clerk
2 111500.00 manager
4 74250.00 salesman
3 rows in set (0.00 sec)

23. Display the total salary paid in each department.


mysql> select sum(sal) totalsal,dname from dept, emp where dept.deptno=emp.deptno
group by dname;
totalsal dname
135750.00 admin
60295.00 HR
29750.00 PURCHASE
44500.00 SALES
4 rows in set (0.00 sec)

24. List the higest salary paid for a manager and clerk.
mysql> select max(sal),job from emp group by job having job in ('manager','clerk');
max(sal) job
Clerk
80000.00
55750.00 manager
2 rows in set (0.00 sec)
25. Increment the salary of all the employees by 5000 and change the mgr to 2000 where
the mgr is not entered.
mysql> update emp set sal=sal+5000, mgr=2000 where mgr is null;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select * from emp;

empi ename job mgr hiredate sal comm deptno


d
7369 Smith Clerk 7902 2018-12-17 80000.00 1000 10
7498 allen 7698 2018-02-20 16000.00 300 30
salesman
7499 allen 7698 2020-02-20 16000.00 3000 30
salesman
7521 ward 7698 2019-02-22 12500.00 500 30
ANNMARYKURUVILLA XIIA

C1-3 7
salesman
7566 joes manager 7839 2020-04-02 55750.00 1000 20
7654 martin 7698 2020-09-28 29750.00 1000 40
salesman
7698 blake clerk 2000 1980-01-03 9545.00 NULL 20
7782 clark manager 7839 1980-05-09 55750.00 3000 10
8 rows in set (0.00 sec)

ANNMARYKURUVILLA XIIA

C1-3 8

You might also like