SQL query exercises

Here is a basic SQL query exercises are for self-study, just to get the students to practice using SQL programming

 

Database has three tables, student tables of students, course enrollment table, sc score table.

The following questions (answers pull down, finished look to answer oh):

1. each query all data tables students and students in curriculum repair.

2. Query results school student number, course number, and scores between 70-80 points.

3. Query highest number of scores C01 course grade

4. Query students enrolled in courses which require listed course number

5. All students course number C02 queries grade point average, the highest score and lowest score.

6. The number of students in each department of statistics

7. Statistical each course and the number of Course exams highest score

8. The number of student enrollment statistics for each gate, according to ascending order of the number of elective door display results

9. Statistical elective total number of students grade point average and test

10. Query grade point average and the number of elective elective door gate count of more than two students

11. Listing a total score of over 200 points of students asked to list student number, total score

12. Name and location-based queries elective course number C02 students.

13. The above query score 80 points in the student's name, course number and grades, according to the results in descending order of performance.

14. querying computer repair Boys 'database foundation' of the student's name, gender, achievement

15. The same query what the student's age, the same age of the students asked to list the names and ages.

16. What course did not query the candidates, asked to list the course number and course name.

17. The inquiry has test scores of all the student's name, name of Course and examination results.

- Requires the query results in a new permanent table (assuming that the new table called new-sc) in.

18. The name of the student information system, respectively, query and computer science, gender, Course Name, Course results

19. The sub-query to achieve the following query:

Name and location-based (1) the query elective course number C01 students

(2) Department of Mathematics student achievement query number and name of school for more than 80 minutes.

Students selected (3) Department of Computer Science course name query

20. The computer-based student score higher than 80 is inserted into the Course where another table, two cases implemented:

(1) In the table built during insertion

(2) to build a new table, and then insert the data

21. Delete Course score less than 50 points the student's Course Record

22. All students enrolled in the course C01 scores 10 points.


Do first question, the answer look at Oh! I believe you will certainly be nice to finish no small gain of ~


SQL code is as follows:

CREATE TABLE student
(Sno    varchar(20)    NOT NULL,
 Sname  varchar(20)    NOT NULL,
 sex   varchar(20)    NOT NULL,
 age   INT            NOT NULL,
 dept  varchar(20)    NOT NULL,
 PRIMARY KEY (Sno)
);

CREATE TABLE course
(Cno    varchar(20)    NOT NULL,
 Cname  varchar(20)    NOT NULL,
 hours VARCHAR(20)    NOT NULL,
 PRIMARY KEY (Cno)
);

CREATE TABLE SC
(Sno    varchar(20)    NOT NULL,
 Cno    varchar(20)    NOT NULL,
 grade  INT    ,
 PRIMARY KEY (Sno,Cno)
);

INSERT INTO student VALUES ('9512101','李勇','男',19,'计算机系');
INSERT INTO student VALUES ('9512102','刘晨','男',20,'计算机系');
INSERT INTO student VALUES ('9512103','王敏','女',20,'计算机系');
INSERT INTO student VALUES ('9521101','张立','男',22,'信息系');
INSERT INTO student VALUES ('9521102','吴宾','女',21,'信息系');
INSERT INTO student VALUES ('9521103','张海','男',20,'信息系');
INSERT INTO student VALUES ('9531101','钱小力','女',18,'数学系');
INSERT INTO student VALUES ('9531102','王大力','男',19,'数学系');


INSERT INTO course VALUES ('C01','计算机文化学','70');
INSERT INTO course VALUES ('C02','VB','90');
INSERT INTO course VALUES ('C03','计算机网络','80');
INSERT INTO course VALUES ('C04','数据库基础','108');
INSERT INTO course VALUES ('C05','高等数学','180');
INSERT INTO course VALUES ('C06','数据结构','72');


INSERT INTO SC VALUES ('9512101','C01',90);
INSERT INTO SC VALUES ('9512101','C02',86);
INSERT INTO SC VALUES ('9512101','C06',NULL);
INSERT INTO SC VALUES ('9512102','C02',78);
INSERT INTO SC VALUES ('9512102','C04',66);
INSERT INTO SC VALUES ('9521102','C01',82);
INSERT INTO SC VALUES ('9521102','C02',75);
INSERT INTO SC VALUES ('9521102','C04',92);
INSERT INTO SC VALUES ('9521102','C05',50);
INSERT INTO SC VALUES ('9521103','C02',68);
INSERT INTO SC VALUES ('9521103','C06',NULL);
INSERT INTO SC VALUES ('9531101','C01',80);
INSERT INTO SC VALUES ('9531101','C05',95);
INSERT INTO SC VALUES ('9531102','C05',85);

 

 

- 1. The students were all data tables and queries of students in the revised curriculum

SELECT * FROM student
SELECT * FROM course

 

- 2. Query score between 70-80 points at the student's student number, course number and results

SELECT *
FROM SC 
WHERE grade > 70 and grade < 80

 

- 3. Query highest number of scores C01 course grade

SELECT max(grade)
FROM SC 
WHERE Cno = 'C01'
GROUP BY Cno

 

- 4. Query students enrolled in courses which require listed course number

SELECT Cno
FROM SC 
GROUP BY Cno

 

- The number of inquiries for all students C02 course grade point average, the highest score and lowest score.

SELECT AVG(grade),MAX(grade),MIN(grade)
FROM SC 
WHERE Cno = 'C02'
group BY Cno

 

- 6. The number of students in each department of statistics

SELECT dept,count(*) as `人数`
FROM student
GROUP BY dept

 

- 7. Statistical each course and the number of Course exams highest score

SELECT Cno, count(*),max(grade)
FROM SC 
group BY Cno

 

- 8. The number of statistics for each student elective door, according to ascending order of the number of elective door display results

SELECT Sno, count(*)
FROM SC 
GROUP BY Sno
ORDER BY count(*)

 

- 9. Statistical elective course grade point average total number of students and examinations

SELECT Cno, count(*), avg(grade)
FROM SC 
GROUP BY Cno

 

- 10. Query elective gate count of more than two of the students' grade point average and the number of elective door

SELECT Sno, count(*),avg(grade)
FROM SC 
GROUP BY Sno
having count(*) > 2

 

- 11 lists the total score over 200 points of students asked to list student number, total score

SELECT Sno,sum(grade)
FROM SC 
GROUP BY Sno
HAVING sum(grade) > 200

 

- 12. Name and location-based queries elective course number C02 students.

SELECT student.Sname,student.dept,SC.Cno
FROM student join SC ON student.Sno = SC.Sno   --  选取内容在两张表中,第一种方法将两张表联结起来
WHERE Cno = 'C02'

 

SELECT Sname,dept
FROM student
WHERE Sno in (SELECT Sno 
              FROM SC 
              WHERE Cno = 'C02')               -- 第二种方法,采用子查询法。

 

- 13. Query results above 80 points the student's name, course number and grades, according to the results in descending order of performance.

SELECT S.Sname,SC.Cno,SC.grade
FROM student AS S JOIN SC ON S.Sno = SC.Sno
WHERE SC.grade > 80
ORDER BY SC.grade DESC

 

 

- 14. querying computer repair Boys 'database foundation' of the student's name, gender, achievement

SELECT S.Sname,S.sex,SC.grade
FROM student AS S JOIN SC ON S.Sno = sc.Sno
                  JOIN course ON sc.Cno = course.Cno
WHERE S.dept = '计算机系' AND S.sex = '男' AND course.Cname = '数据库基础'

 

SELECT Sname,sex,SC.Grade
FROM student
inner join SC ON Cno IN(SELECT Cno FROM course WHERE Cname='数据库基础') -- 显示成绩的条件,仅联结了两张表,On 后面也可以跟不同的联结条件。
AND student.Sno=SC.Sno -- 显示成绩的学生的学号
WHERE dept='计算机系' AND sex='男' 

---------------------

 

- 15. Queries which students of the same age, the same age students asked to list the names and ages.

SELECT S1.Sname,S1.sex
FROM student AS S1, student AS S2
WHERE S1.age = S2.age AND S1.Sname <> S2.Sname
GROUP BY S1.Sname
ORDER BY S1.age 

 

- 16. What course did not query the candidates, asked to list the course number and course name.

SELECT Cno,Cname
FROM course
WHERE Cno NOT in (SELECT Cno 
                  FROM sc
                  GROUP BY Cno)

 

- 17. The query has test scores of all the student's name, name of Course and examination results.

- Requires the query results in a new permanent table (assuming that the new table called new-sc) in.

CREATE TABLE new_sc AS                                          -- 将查询结果放在新表中的方法。
SELECT S.Sname,course.Cname,sc.grade 
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
                  INNER JOIN course ON course.Cno = sc.Cno
WHERE grade IS NOT NULL

 

- 18 respectively Query student's name and information department of computer science, gender, Course Name, Course results

SELECT S.Sname,S.sex,course.Cname,sc.grade
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
                  INNER JOIN course ON course.Cno = sc.Cno
WHERE S.dept = '信息系'
UNION
SELECT S.Sname,S.sex,course.Cname,sc.grade
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
                  INNER JOIN course ON course.Cno = sc.Cno
WHERE S.dept = '计算机系'

 

- 19. The sub-query to achieve the following query:

- (1) the query elective course number C01 of the student's name and location system

SELECT Sname,dept
FROM student
WHERE Sno in (SELECT Sno
              FROM sc
               WHERE Cno = 'C01')

 

- (2) inquiry-based math than students 80 school number and name.

SELECT Sno,Sname
FROM student
WHERE dept = '数学系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)

 

- Course students selected (3) Department of Computer Science inquiry

SELECT Cname
FROM course
WHERE Cno in (SELECT Cno 
              FROM sc 
              where Sno in (SELECT Sno 
                            FROM student 
                            WHERE dept = '计算机系'))

 

- 20. A computer-based student score higher than 80 is inserted into the Course where another table, two cases implemented:

- (1) during insertion of the build table

SELECT Sno,Sname
FROM(
SELECT Sno,Sname
FROM student
WHERE dept = '数学系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)) AS newtable

 

- (2) to build a new table, and then insert the data

CREATE TABLE newtable2 AS
SELECT Sno,Sname
FROM student
WHERE dept = '数学系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)

 

- 21. Course delete score less than 50 points the student's Course Record

DELETE FROM sc
WHERE grade < 50 or grade IS NULL

 

- 22. All students enrolled in the course C01 scores 10 points.

UPDATE sc
SET grade = grade + 10
WHERE Cno = 'C01'

Guess you like

Origin www.cnblogs.com/zquan/p/12596157.html