Skip to content

Commit 48c2372

Browse files
aggregate function
1 parent b30c131 commit 48c2372

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

Diff for: first.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,6 @@ select * from products order by productname desc;
379379
drop table products;
380380
drop table categories;
381381

382+
use mydatabase;
382383

383-
384+
select * from categories join products;

Diff for: groupby.sql

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
create database groupdb;
2+
use groupdb;
3+
create table students(roll int primary key auto_increment,
4+
name varchar(30),course varchar(30),marks varchar(40),dept varchar(30));
5+
6+
select * from students;
7+
truncate students;
8+
9+
select * from students group by dept ;
10+
11+
select count(*) from students;
12+
select distinct count(*) from students;
13+
select min(marks) from students;
14+
select max(marks) from students;
15+
select avg(marks) from students;
16+
select sum(marks)/avg(marks) as 'count' ,count(*) from students;
17+
18+
set @num1=50,@num2=30;
19+
20+
select @num1+@num2 as 'sum';
21+
22+
prepare stmt1 from
23+
'select ? + ? as sum ;';
24+
25+
execute stmt1 using @num1,@num2;
26+
27+
deallocate prepare stmt1;

Diff for: join.sql

+33
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,36 @@ union
101101
select * from categories c
102102
right join products p
103103
on c.categoryid=p.categoryid;
104+
105+
106+
-- ----------------------------
107+
-- 04/08/2021 joining multiple tables
108+
use mydatabase;
109+
110+
create table user_info(userid int primary key auto_increment,
111+
username varchar(20),password varchar(20),productid varchar(20),
112+
foreign key(productid) references products(product_id));
113+
114+
insert into user_info(username,password,productid) values('aman','123','201'),('rohan','456','203'),('rohit','789','204');
115+
116+
select * from categories c
117+
inner join products p on c.categoryid=p.categoryid
118+
inner join user_info u on u.product_id=p.product_id;
119+
120+
-- or
121+
alter table user_info rename column productid to product_id;
122+
123+
select * from categories c
124+
inner join products p using(categoryid)
125+
inner join user_info u using(product_id);
126+
-- --------------------------------
127+
128+
select * from categories c
129+
left join products p
130+
on c.categoryid=p.categoryid
131+
where p.categoryid is null
132+
union
133+
select * from categories c
134+
right join products p
135+
on c.categoryid=p.categoryid
136+
where c.categoryid is null;

Diff for: sql2.xlsx

6.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)