-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL DAY 3 08-09-2021.sql
147 lines (117 loc) · 3.73 KB
/
SQL DAY 3 08-09-2021.sql
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
select * from employees
order by salary;
select * from employees
order by salary desc;
select * from employees
order by salary desc,first_name asc;
select * from employees
order by first_name asc,salary desc;
#AGGREGATE FUNCTION:-
/* WAQ TO DISPLAY THE AVG SALARY OF THE EMPLOYEES IN THE COMPANY*/
SELECT avg(SALARY)
FROM employees;
/*
5.WAQ TO DISPLAY THE TOTAL NUMBER OF EMPLOYESS WORKING IN THE COMPANY?
*/
SELECT COUNT(EMPLOYEE_ID)
FROM employees;
SELECT count(*) FROM employees;
SELECT COUNT(COMMISSION_PCT)
FROM EMPLOYEES;
#6. WAQ TO DISPLAY THE MIN SALARY DRAWN,MAXIMUM SALARY DRAWN AND AVERAGE SALARY DRAWN IN THE COMPANY.
select min(SALARY) AS MINIMUM_SALARY,max(SALARY) AS MAXIMUM_SALARY,AVG(SALARY) AS AVERAGE_SALARY
FROM EMPLOYEES;
#7. WAQ TO DISPLAY THE TOTAL NUMBER OF EMPLOYEES WORKING IN EACH DEPARTMENT IN THE COMPANY
SELECT department_id,COUNT(employee_id)
FROM employees
group by department_id;
SELECT count(distinct department_id)
FROM EMPLOYEES;
SELECT department_id,employee_id
FROM employees;
SELECT MAX(SALARY) FROM employees
WHERE department_id=10;
/*
#8.WAQ TO DISPLAY THE TOTAL NUMBER OF EMPLOYEES WORKING IN EACH DEPARTMENT UNDER EACH JOB_ID IN THE COMPANY
*/
select department_id,job_id,count(employee_id)
FROM EMPLOYEES
group by department_id,job_id;
#9.WAQ TO DISPLAY THE TOTAL AMOUNT OF MONEY TO BE BLOCKED AS BUDGET FOR PROVIDING SALARY TO ALL THE EMPLOYEES IN EVERY DEPARTMENT.
DESC employees;
SELECT department_id,sum(SALARY) AS BUDGET
FROM employees
group by department_id;
/*STAEMENT SKELETON
SELECT...4TH EXECUTION
FROM...1ST EXECUTION FIRST
WHERE...2ND EXECUTION
GROUP BY...3RD EXECUTION
ORDER BY...5TH EXECUTION
LIMIT...6TH EXECUTION
#FROM-WHERE-GROUP BY-SELECT-ORDER BY-LIMIT
*/
#FILTERING:-WHERE-NORMAL/HAVING-AGGREGATE
#Q10.WAQ TO DISPLAY THE TOTAL NUMBER OF EMPLOYEES WORKING IN EACH DEPARTMENT IN THE COMPNAY.DISPLAY THE DEPARTMENT WHERE THERE ARE MORE THAN 10 EMPLOYESS WORKING.
/*SOLUTION*/
SELECT DEPARTMENT_ID,count(employee_id)
FROM employees
group by department_id;# BUT ANSWER HAVE NOT ARRIVED
#WHERE-FILTERING ONLY NORMAL VALUE,BUT FOR AGGREGATE 'WHERE' CANNOT BE USED SO WE USE 'HAVING'
/* NOW WE USE HAVING*/
SELECT department_id,COUNT(employee_id)
FROM employees
group by department_id
HAVING COUNT(employee_id)>10;
SELECT department_id,COUNT(employee_id)
FROM employees
group by department_id
HAVING COUNT(employee_id)>10 AND SUM(SALARY)>200000;
/*
FOREIGN KEY
-COLUMN1 TABLE WHICH IS REFERENCING THE PRIMARY KEY OF THE OTHER TABLE
-IT HELPS US IDENTIFY THE RELATIONSHIP B/W 2 TABLES/ENTITES
-THE TABLE WITH FOREIGN KEY IS CALLED CHILD TABLE.
-THE TABLE WHOSE PRIMARY KEY WE ARE REFERENCING IS CALLED PARENT TABLE.
FOREIGN KEY WILL ALLOW THE VALUES WHICH ARE PRESENT IN THE PRIMARY KEY OF THE OTHER TABLE AND NULL
CREATE TABLE TABLE_NAME
(
COLNAME1 DATATYPE CONSTRAINTNAME,
COLNAME2 DATATYPE CONSTRAINTNAME,
COLNAME3 DATATYPE CONSTRAINTNAME,
,
,
FOREIGN KEY(COLNAME) REFERENCES PARENT_TABLE_NAME(PRIMARY KEY)
);
ALTER TABLE TABLE_NAME ADD FOREIGN KEY(COLNAME) REFERENCES PARENT_TABLE_NAME(PK_COL_NAME);
*/
/*
#Q11.WAQ
CREATE A COURSE TABLE AS CID PK,CNAME,DESCRIPTION
STUDENT_DETAILS-SID PK,SNAME,PHNO,EMAILID,COURSE_ID,ADDRESS
#NOTE:COURSE ID FROM THE STUDENT_DETAILS TABLE SHOULD ACTAS FOREIGN KEY AND LINK TO THE COURSE TABLE.
*/
USE chn_jul21;
CREATE table COURSE
(
cid int,
cname varchar(20),
description varchar(50),
PRIMARY KEY (cid)
);
create table studentdetails
(
sid int primary key ,
sname varchar(50),
phno varchar(20),
emailid varchar(50),
address varchar(50),
cid int,
foreign key(cid) REFERENCES course(cid)
);
describe studentdetails;
describe COURSE;
select e.employee_id,e.first_name,e.last_name,e.department_id,d.department_name
from employees e
join departments d
on e.department_id=d.department_id;