-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Exercise6.txt
More file actions
146 lines (125 loc) · 4.22 KB
/
SQL_Exercise6.txt
File metadata and controls
146 lines (125 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
1. Display all the Suppliers with the same Status as the supplier, ‘ARJUN’.
SELECT SNAME FROM S
WHERE `STATUS`=
(SELECT `STATUS` FROM S
WHERE SNAME='ARJUN');
----->
+--------+
| SNAME |
+--------+
| ARJUN |
| ANIL |
| ATUL |
| GOVIND |
| KAPIL |
+--------+
5 rows in set (0.00 sec)
==============================================================================================================
2. Display all the Employees in the same department as the employee ‘MILLER’.
===============================================================================================================
3. Display all the Parts which have more Weight than all the Red parts.
SELECT * FROM P
WHERE WEIGHT >
(SELECT MAX(WEIGHT) FROM P
WHERE COLOR='RED');
----->
+------+--------+--------+--------+--------+
| P# | PNAME | COLOR | WEIGHT | CITY |
+------+--------+--------+--------+--------+
| P5 | PRT 4 | WHITE | 4 | ATHENS |
| P7 | PRT 9 | YELLOW | 7.1 | PARIS |
| P10 | PRT 10 | BLUE | 4 | LONDON |
+------+--------+--------+--------+--------+
3 rows in set (0.00 sec)
=================================================================================================================
4. Display all the Projects going on in the same city as the project ‘PROJ 4’.
SELECT * FROM J
WHERE CITY=
(SELECT CITY FROM J
WHERE JNAME='PROJ 4');
----->
+------+--------+--------+
| J# | JNAME | CITY |
+------+--------+--------+
| J4 | PROJ 4 | BERLIN |
| J5 | PROJ 6 | BERLIN |
+------+--------+--------+
2 rows in set (0.00 sec)
==================================================================================================================
5. Display all the Parts with Weight less than all the Green parts.
SELECT * FROM P
WHERE WEIGHT<
(SELECT MIN(WEIGHT) FROM P
WHERE COLOR='GREEN');
----->
Empty set (0.00 sec)
SELECT * FROM P
WHERE WEIGHT<
(SELECT MIN(WEIGHT) FROM P
WHERE COLOR='BLUE');
----->
+------+-------+-------+--------+--------+
| P# | PNAME | COLOR | WEIGHT | CITY |
+------+-------+-------+--------+--------+
| P2 | PRT 2 | RED | 1 | PARIS |
| P4 | PRT 6 | GREEN | 0.5 | PARIS |
| P8 | PRT 8 | BLACK | 2 | BERLIN |
+------+-------+-------+--------+--------+
3 rows in set (0.00 sec)
===================================================================================================================
6. Display the name of the Supplier who has sold the maximum Quantity (in one sale).
SELECT SNAME FROM SPJ, S
WHERE S.`S#`=SPJ.`S#`
AND QTY=
(SELECT MAX(QTY) FROM SPJ);
----->
+--------+
| SNAME |
+--------+
| GOVIND |
+--------+
1 row in set (0.00 sec)
===================================================================================================================
7. Display the name of the Employee with the minimum Salary.
SELECT NAME FROM EMP
WHERE SAL=
(SELECT MIN(SAL) FROM EMP);
----->
+------+
| NAME |
+------+
| Devi |
+------+
1 row in set (0.00 sec)
====================================================================================================================
8. Display the name of the Supplier who has sold the maximum overall Quantity (sum of Sales).
SELECT SNAME, SUM(QTY) FROM SPJ, S
WHERE SPJ.`S#`=S.`S#`
GROUP BY SNAME
HAVING SUM(QTY)=
(SELECT MAX(SUM) FROM
(SELECT SUM(QTY) AS "SUM" FROM SPJ
GROUP BY SPJ.`S#`)AS TEMP);
----->
+-------+----------+
| SNAME | SUM(QTY) |
+-------+----------+
| VIPUL | 1310 |
+-------+----------+
1 row in set (0.00 sec)
======================================================================================================================
9. Display the name of the Department with the maximum number of Employees.
SELECT DNAME , COUNT(EMPNO) AS "TOTAL EMP" FROM DEPT, EMPT
WHERE DEPT.DEPTNO=EMPT.DEPTNO
GROUP BY DNAME
HAVING COUNT(EMPNO)=
(SELECT MAX(TOTAL) FROM
(SELECT COUNT(EMPNO) AS "TOTAL" FROM EMPT
GROUP BY DEPTNO) AS TEMP);
----->
+-------+-----------+
| DNAME | TOTAL EMP |
+-------+-----------+
| TRN | 3 |
+-------+-----------+
1 row in set (0.00 sec)