Skip to content

Commit bb5f87a

Browse files
Update SQL_Class_6.txt
1 parent 613fb2f commit bb5f87a

1 file changed

Lines changed: 73 additions & 3 deletions

File tree

SQL_Class_6.txt

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,77 @@ create view department_wise_salary as select dept_name, sum(salary) as total_sal
3232

3333
select * from department_wise_salary;
3434

35-
36-
37-
35+
# Union and Union all
36+
37+
create table student
38+
(
39+
stu_id int,
40+
name varchar(50),
41+
email varchar(50),
42+
city varchar(50)
43+
);
44+
45+
insert into student values(1,'Shashank','[email protected]', 'lucknow');
46+
insert into student values(2,'Rahul','[email protected]', 'mp');
47+
insert into student values(3,'Amit','[email protected]', 'noida');
48+
insert into student values(4,'Nikhil','[email protected]', 'bangalore');
49+
insert into student values(5,'Sunny','[email protected]', 'bangalore');
50+
51+
create table student2
52+
(
53+
stu_id int,
54+
name varchar(50),
55+
email varchar(50),
56+
city varchar(50)
57+
);
58+
59+
60+
insert into student2 values(1,'Shashank','[email protected]', 'lucknow');
61+
insert into student2 values(6,'Anuj','[email protected]', 'mp');
62+
insert into student2 values(8,'Mohit','[email protected]', 'noida');
63+
insert into student2 values(10,'Sagar','[email protected]', 'bangalore');
64+
insert into student2 values(5,'Sunny','[email protected]', 'bangalore');
65+
66+
select * from student2;
67+
68+
69+
--- We are organizing an tournament between College-1 and College-2, we need details of all students from both college
70+
select * from student
71+
UNION
72+
select * from student2;
73+
74+
--- how to use union all
75+
select * from student
76+
UNION ALL
77+
select * from student2;
78+
79+
--- Case 1 - Not Failed
80+
81+
select stu_id, `name` from student
82+
UNION
83+
select `name`, stu_id from student2;
84+
85+
--- Case 2 - Not Failed
86+
select stu_id, name from student
87+
UNION
88+
select stu_id, name from student2;
89+
90+
--- Case 3 - Not Failed
91+
select stu_id as stu_id_college_1 , name from student
92+
UNION
93+
select stu_id as stu_id_college_2, name from student2;
94+
95+
--- Case 4 - Not Failed
96+
select stu_id from student
97+
UNION
98+
select email from student2;
99+
100+
101+
select 1 as salary
102+
UNION
103+
select 'Shashank' as name;
104+
105+
select 1 as salary, 'Hello' as message
106+
UNION
107+
select 'Shashank' as name;
38108

0 commit comments

Comments
 (0)