1、 select * from student a left join class b on a.c_id = b.id where score = ( select max(score) from student)
2、 select b.name,count(*),avg(score) as avgScore from student a,class b where a.c_id = b.id group by b.name order by avgScore;
2、Table A保存客戶的基本信息;Table B 保存客戶的資產負債信息。按要求寫sql
答案:
(1)select ID from A where name = '李四'; (2)select NO,NAME,ID from A,B where A.NO = B.NO AND DEPOSIT >= 1000; (3)select COUNTRY,count(*) FROM A GROUP BY COUNTRY;
(4)SELECT '80后' as 年齡段,sum(CREDIT) AS '信用卡余額' FROM A,B WHERE A.NO = B.NO AND BIRTH >= 19800101 AND BIRTH < 19900101 UNION SELECT '90后' as 年齡段,sum(CREDIT) AS '信用卡余額' FROM A,B WHERE A.NO = B.NO AND BIRTH >= 19900101 AND BIRTH < 20000101;
3、數據庫(編寫示例sql)
答案:
select orderNo, if(status=1,'新建','處理中') from OrderTrans;
select DATE_FORMAT(a.crttime,'%m-%d-%Y') ,username,count(*) from testCase a,User b where a.crtUser = b.UserId group by DATE_FORMAT(a.crttime,'%m-%d-%Y'),b.username, having status = '成功';
select count(*),sum(amount),DATE_FORMAT(crttime,'%m-%d-%Y') from OrderInfo group by DATE_FORMAT(crttime,'%m-%d-%Y');
Select * from students where jg = ‘湖北’ and birthday = ‘1992-6-1’ order by no asc;
Select avg(so.cj),min(so.cj),max(so.cj),sum(so.cj) from student st,course c,source so where st.no = so.no and c.kebh=so.kebh and st.name = ‘王華’ group by st.no;
Select st.no,st.name,st.bj,c.kebh,c.kcmc,so.cj from student st,course c,source so where st.no = so.no and c.kebh=so.kebh and st.name = ‘張三’ order by so.cj desc;
Select Consultant.SubsidiaryID, BaseOrder.* from Consultant, BaseOrder where Consultant.ConsultantID = BaseOrder.ConsultantID and Consultant.SubsidiaryID = 29 order by Orderid desc;
Select BaseOrder.ConsultantID,sum(OrderDetails.TotalPrice) from BaseOrder,OrderDetails where BaseOrder.Orderid=OrderDetails.Orderid and month(OrderDate) = 5 group by BaseOrder.ConsultantID;
Insert into Consultant (ConsultantID,ConstultantStatusID,SubsidiaryID,Name) values (200000,10,29,’Gary’);
Delete from Consultant where Name like ‘%Gary%’;
Update BaseOrder set OrderDate = sysdate() where ConsultantID in (select ConsultantID from BaseOrder where ConsultantID=100003 order by OrderDate desc limit 0,1);