Use of group by, count, and inner join queries in SQL

Source: Internet
Author: User
Reference Problem description:
There are three tables:
Student Information table S (sno, sname)
Course information table C (cno, cname, cteacher)
Student-course relationship table SC (sno, cno, scroce)
Question 1: find out the names of all students who have not taken the course taught by Mr. Li Ming.
Question 2: The name of two or more failed students is displayed.
Question 3: find out the names of students who have selected both course "1" and course "2 ".

Main test: group by, inner join, having filtering, and use of the count function
Group by groups result sets and uses having to filter data,

SQL code

  1. Drop table S, SC, C
  2. Create table S (
  3. Sno varchar (15), -- Student ID
  4. Sname varchar (15) -- Student name
  5. )
  6. Create table C (
  7. Cno varchar (15), -- course No.
  8. Cname varchar (15), -- Course name
  9. Cteacher varchar (15)-Instructor
  10. )
  11. Create table SC (
  12. Sno varchar (15), -- Student ID
  13. Cno varchar (15), -- course No.
  14. Scroce float -- score
  15. )
  16. Select * from S;
  17. Delete from S;
  18. Insert into S (sno, sname) values ('s001', 'Jerry ');
  19. Insert into S (sno, sname) values ('s002 ', 'Tom ');
  20. Insert into S (sno, sname) values ('s003 ', 'jason ');
  21. Delete from C;
  22. Select * from C;
  23. Insert into C (cno, cname, cteacher) values ('c001', '1', 'lilim ');
  24. Insert into C (cno, cname, cteacher) values ('c002 ', '2', 'Li Ming 0 ');
  25. Insert into C (cno, cname, cteacher) values ('c003 ', '3', 'Li Ming 1 ');
  26. Insert into C (cno, cname, cteacher) values ('c004 ', '4', 'Li Ming 2 ');
  27. Delete from SC;
  28. Select * from SC;
  29. Insert into SC (sno, cno, scroce) values ('s001', 'c001', 40 );
  30. Insert into SC (sno, cno, scroce) values ('s001', 'c002', 50 );
  31. Insert into SC (sno, cno, scroce) values ('s002', 'c001', 50 );
  32. Insert into SC (sno, cno, scroce) values ('s002 ', 'c003', 50 );
  33. Select
  34. A. sno, a. sname, B. cno
  35. From S as
  36. Inner join SC as B on B. sno = a. sno
  37. Where not exists (
  38. Select * from SC where sno = B. sno and cno in (select cno from C where cteacher = 'liming ')
  39. );
  40. -- Two failing students
  41. Select
  42. A. sname
  43. From S as
  44. Inner join SC as B on B. sno = a. sno
  45. Where B. scroce <60
  46. Group by a. sname
  47. Having count (sname)> 1;
  48. -- Names of students who have selected course 1 and course 2
  49. Select
  50. A. sname
  51. From S
  52. Inner join SC B on A. Sno = B. SnO
  53. Inner join C on C. CNO = B. CNO
  54. Where B. CNO in ('c001', 'c002 ')
  55. Group by A. sname
  56. Having count (A. sname) = 2;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.