Experiment 3 simple query

Table of contents

1. Level 1: Simple query 1

1.1 Task description

1.2 SQL statement

2. Level 2: Simple query 2

2.1 Task description

2.2 SQL statements

3. Level 3: Simple query 3

3.1 Task description

3.2 SQL statement


1. Level 1: Simple query 1

1.1 Task description

Use the Select statement in the SQL statement to query data:

1. Query the student number, name, and age of male students in the CS department;

2. Query the course selection information of students who fail, list the student number, course number, and grades;

3. Query the courses whose predecessor courses are not empty (use * to indicate the query result);

4. Query the student number and name of the student with the letter 'n' in the name (using the like statement);

5. Use the distinct keyword to query different departments in the student table, and list the departments (remove duplicate ancestors).

1.2 SQL statement

SELECT sno,sname,sage FROM Student WHERE Sdept='CS' AND Ssex='m';
SELECT sno,cno,grade FROM SC WHERE Grade<60;
SELECT * FROM Course WHERE Cpno is NOT NULL;
SELECT sno,sname FROM Student WHERE Sname LIKE '%n%';
SELECT DISTINCT sdept FROM Student;

2. Level 2: Simple query 2

2.1 Task description

1. Query the course selection information of students with a score of 90 or above, and list the student number, name, course number, and grades;

2. Query the course selection of the 'DB' course, list the student number and grades.

2.2 SQL statements

SELECT SC.sno,sname,cno,grade FROM Student,SC 
WHERE Student.Sno=SC.Sno AND Grade>90;
--在两个表中查询数据,需要建立等式连接
SELECT sno,grade FROM SC WHERE Cno=(SELECT Cno FROM Course WHERE Cname='DB');
-- 存在子查询语句

3. Level 3: Simple query 3

3.1 Task description

1. Query the situation of the students in the CS department who choose the 'DB' course, list the student number and grades;

2. Check the course selection of female students, list the student number, course number, course name, and grades.

3.2 SQL statement

SELECT sno,grade FROM SC WHERE Cno=
(SELECT Cno FROM Course WHERE Cname='DB') 
AND sno IN (SELECT Sno FROM Student WHERE Sdept='CS');
SELECT sno,SC.cno,cname,grade FROM SC,Course WHERE Sno IN 
(SELECT Sno FROM Student WHERE Ssex='f') AND SC.Cno=Course.Cno;

Guess you like

Origin blog.csdn.net/weixin_62707591/article/details/130811635