-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsqlquery19.sql
56 lines (51 loc) · 3.19 KB
/
sqlquery19.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
Problem Statement:
Formulate a MySQL query to list out all the projects(id, name) and employee's names (first, last) along with their respective Email id’s irrespective of the fact if that project is assigned or not and whether an employee is assigned any project or none.
Information about the table:
Table Employee:
+-------+-----------+----------+------+-------------------+-----------+-----------+
| EmpID | EmpFname | EmpLname | Age | EmailID | PhoneNo | City |
+-------+-----------+----------+------+-------------------+-----------+-----------+
| 1 | Riya | Khanna | 21 | [email protected] | 987655443 | Delhi |
| 2 | Sahil | Kumar | 32 | [email protected] | 987657643 | Mumbai |
| 3 | Vishwas | Aanand | 24 | [email protected] | 987658871 | Kolkata |
| 4 | Harleen | Kaur | 27 | [email protected] | 987677585 | Bengaluru |
| 5 | Priyanshu | Gupta | 23 | [email protected] | 956758556 | Hyderabad |
+-------+-----------+----------+------+-------------------+-----------+-----------+
Table Project:
+-----------+-------+-------------+------------------+----------+
| ProjectID | EmpID | ProjectName | ProjectStartDate | ClientID |
+-----------+-------+-------------+------------------+----------+
| 100 | 1 | pro_1 | 2021-04-21 | 3 |
| 200 | 2 | pro_2 | 2021-03-12 | 1 |
| 300 | 3 | pro_3 | 2021-01-16 | 5 |
| 400 | 3 | pro_4 | 2021-04-27 | 2 |
| 500 | 5 | pro_5 | 2021-05-01 | 4 |
| 600 | 9 | pro_6 | 2021-01-19 | 1 |
| 700 | 7 | pro_7 | 2021-08-27 | 2 |
| 800 | 8 | pro_8 | 2021-09-15 | 3 |
+-----------+-------+-------------+------------------+----------+
Hint: Use Full Join, but MySql doesn’t support the “Full Join” clause.
Note-1: Write keywords of syntax in uppercase alphabets.
Note-2: Use employee ID to link the two tables.
Solution:
SELECT p.projectid, p.projectname, e.empfname, e.emplname, e.emailid FROM project p
LEFT JOIN employee e
ON p.empid = e.empid
UNION
SELECT p.projectid, p.projectname, e.empfname, e.emplname, e.emailid FROM project p
RIGHT JOIN employee e
ON p.empid = e.empid;
Output:
+-----------+-------------+-----------+----------+-------------------+
| projectid | projectname | empfname | emplname | emailid |
+-----------+-------------+-----------+----------+-------------------+
| 100 | pro_1 | Riya | Khanna | [email protected] |
| 200 | pro_2 | Sahil | Kumar | [email protected] |
| 300 | pro_3 | Vishwas | Aanand | [email protected] |
| 400 | pro_4 | Vishwas | Aanand | [email protected] |
| 500 | pro_5 | Priyanshu | Gupta | [email protected] |
| 600 | pro_6 | NULL | NULL | NULL |
| 700 | pro_7 | NULL | NULL | NULL |
| 800 | pro_8 | NULL | NULL | NULL |
| NULL | NULL | Harleen | Kaur | [email protected] |
+-----------+-------------+-----------+----------+-------------------+