SQL輕松玩轉Excel的10大功能,最后再來幾道面試題練練手。
來源 : 知乎 轉自:SQL數(shù)據庫開發(fā)

create table sale_guang
SELECT * from sale where city="廣州";
SELECT * from sale a
inner JOIN
(SELECT ordernum,profit from sale_guang) b
on a.ordernum=b.ordernum
SELECT * from sale a
WHERE a.ordernum not in
(SELECT b.ordernum from sale_guang b);
SELECT * FROM sale
where salesnum not in
(SELECT salesnum from sale
GROUP BY salesman
HAVING COUNT(salesnum)>1)
--用0填充:
update sale set city = 0 where city = NULL
--刪除有缺失值的行:
delete from sale where city = NULL;
SELECT * from sale
where salesman = "張愛"
and city = "北京"
and orderaccount >=6000;
SELECT * from sale
where inventoryname like "%三星%"
or 存貨名稱 like "%索尼%";
SELECT city,sum(`profit`)
from sale
WHERE city = "北京"
GROUP BY `city`;
--有多少個?
SELECT COUNT(*) from sale
where inventoryname like "%三星%"
and `tax` > 1000 ;
--這些訂單的利潤總和和平均利潤是多少?
SELECT `ordernum`,SUM(profit),AVG(`profit`)
from sale
where inventoryname like "%三星%"
and `tax` > 1000
GROUP BY `ordernum`;
SELECT trim(inventoryname) from sale;
SELECT city,ordernum,
(Nontaxamount - profit) as cost
from sale
order by cost DESC;
總結:結構化查詢語言(Structured Query Language)簡稱SQL,果然和它名字一樣,查詢起來得心應手,但做想做數(shù)據處理方面,能明細感受到比Python和excel吃力(也可能是我還沒學好orz)。
貼一些在面試時遇到過的SQL筆試題吧:


select sname,ssex,class from student;
select * from score between 60 and 80;
select class,avg(degree) from Score a
join student b
on a.sno = b.sno
GROUP BY CLASS;

create table Student_new
(sno varchar(20) PRIMARY KEY,
sname varchar(10),ssex char(2),
sage int,sdept varchar(25));
select * from student
where sdept = "計算機"
order by sno ;
select a.sno,a.sname,a.ssex from student a
join (Course b ,SC c)
on a.sno=c.sno and b.cno =c.cno
where Ccredit = 5 and Grade > 60;

SELECT a.cus_id from `表a` as a
INNER JOIN `表b` as b
on a.cus_id=b.cus_id;
SELECT * from `表a`
UNION
SELECT * from `表b`;
SELECT * from `表a`
where cus_id not in (SELECT * from `表b`)
UNION
SELECT * from `表b`
where cus_id not in (SELECT * from `表a`);
SELECT * from `表a`
WHERE cus_id not in (SELECT cus_id from `表b`);
點分享 點收藏 點點贊 點在看
評論
圖片
表情





