0% found this document useful (0 votes)
118 views3 pages

VNKTRMNB/DBMS LAB/Nested Queries

The document shows examples of nested queries in MySQL. Nested queries allow a SELECT statement to be used as a subquery in the WHERE clause of another SELECT statement. The examples demonstrate using subqueries to find employees with the maximum salary, salaries greater than a given employee, salaries grouped by department with the maximum in each group, salaries greater than any or all clerks, employees whose salary is greater than their manager's, employees managed by others in their department, employees with duplicate hire dates, and checking for existence or non-existence of employees in departments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views3 pages

VNKTRMNB/DBMS LAB/Nested Queries

The document shows examples of nested queries in MySQL. Nested queries allow a SELECT statement to be used as a subquery in the WHERE clause of another SELECT statement. The examples demonstrate using subqueries to find employees with the maximum salary, salaries greater than a given employee, salaries grouped by department with the maximum in each group, salaries greater than any or all clerks, employees whose salary is greater than their manager's, employees managed by others in their department, employees with duplicate hire dates, and checking for existence or non-existence of employees in departments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Nested Queries

mysql> select ename from emp where sal=(


-> select max(sal) from emp);
+-------+
| ename |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)

mysql> select ename,sal from emp where sal>(select sal from emp where ename='SMITH');
+--------+---------+
| ename | sal |
+--------+---------+
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
13 rows in set (0.00 sec)

mysql> select ename,sal from emp where sal in (select max(sal) from emp group by deptno);
+-------+---------+
| ename | sal |
+-------+---------+
| BLAKE | 2850.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+-------+---------+
4 rows in set (0.00 sec)

mysql> select ename,sal from emp where sal> any (select sal from emp where job='CLERK');
+--------+---------+
| ename | sal |
+--------+---------+
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |

Vnktrmnb/DBMS LAB/Nested Queries


| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
13 rows in set (0.00 sec)
mysql> select ename,sal from emp where sal> all (select sal from emp where job='CLERK');
+--------+---------+
| ename | sal |
+--------+---------+
| ALLEN | 1600.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| FORD | 3000.00 |
+--------+---------+
8 rows in set (0.00 sec)
mysql> select e1.ename from emp e1 where e1.sal>(
-> select e2.sal from emp e2 where e2.empno=e1.mgr);
+-------+
| ename |
+-------+
| SCOTT |
| FORD |
+-------+
2 rows in set (0.00 sec)

mysql> select e1.ename ,e1.deptno


-> from emp e1 where e1.deptno in(select e2.deptno from emp e2 where e2.mgr=e1.empno);
+-------+--------+
| ename | deptno |
+-------+--------+
| FORD | 20 |
| BLAKE | 30 |
| KING | 10 |
| JONES | 20 |
| SCOTT | 20 |
| CLARK | 10 |
+-------+--------+
6 rows in set (0.06 sec)
mysql> select e1.empno,e1.ename from emp e1,emp e2 where e1.empno<>e2.empno and
-> e1.hiredate=e2.hiredate;
+-------+-------+
| empno | ename |
+-------+-------+
| 7902 | FORD |
| 7900 | JAMES |
+-------+-------+
2 rows in set (0.00 sec)

Vnktrmnb/DBMS LAB/Nested Queries


mysql> select d.deptno from dept d where not exists(select * from emp e where e.deptno=d.deptno);
+--------+
| deptno |
+--------+
| 40 |
+--------+
1 row in set (0.00 sec)

mysql> select d.deptno from dept d where exists(select 1 from emp e where e.deptno=d.deptno);
+--------+
| deptno |
+--------+
| 10 |
| 20 |
| 30 |
+--------+
3 rows in set (0.00 sec)

Vnktrmnb/DBMS LAB/Nested Queries

You might also like