SQL query statement practice questions 27

https://blog.csdn.net/friendan/article/details/8072668

The practice environment is: XP+SQL2000 database

The database used in the exercise is: Student Management Database

The database download address is:  http://download.csdn.net/download/friendan/4648150

illustrate

         This is the 27 SELECT statement practice questions given by the teacher when I was studying the database class. When writing this article, the teacher did not give the reference answer.

The purpose of writing this article is to deepen my understanding of SQL statements and facilitate my review and reference in the future.

There are inevitably mistakes in the text, friends who find it, hope to correct one or two, I am grateful.

There are three tables in the database, namely student, course, SC (ie student table, course table, course selection table)

//The screenshots of the three tables are as follows:



--1. Query all the data in the student table and the student course table respectively.
SELECT * FROM student--Query student table
SELECT * FROM course--Query course table


--2. Query the student number, course number and grade of students whose scores are between 70 and 80.
SELECT Sno Student ID, Cno Course ID, Grade
FROM SC
WHERE Grade BETWEEN 70 AND 80

--3. To query the highest score of course C01
SELECT TOP 1 Grade 
FROM SC 
WHERE Cno='C01' 
ORDER BY Grade DESC--descending DESC, ascending ASC --4

. To check which courses students have taken, the course number is required.
SELECT Cname AS student elective courses, Cno AS course number
FROM course
WHERE Cno IN(SELECT DISTINCT Cno FROM SC)--DISTINCT is used to remove duplicates

--5. Query the OrderID, CustomerID and OrderDate of the orders table in the Northwind database,
-- And list the latest order date (OrderDate) in front.
USE Northwind
GO
SELECT OrderID,CustomerID,OrderDate 
FROM orders 
ORDER BY OrderDate DESC--descending DESC, ascending ASC --6


. Query the ShipCountry column of the orders table in the Northwind database with B, C, D, F --//ShipCountry BCDF
-- and the third character is "a" information of OrderID, CustomerID and ShipCountry. //The third character of OrderID and CustomerID is "a"
USE Northwind
GO
SELECT OrderID,CustomerID,ShipCountry FROM orders 
WHERE ShipCountry LIKE '[BCDF]_a%'--_Underscore means any character, % means one or more characters

--7. Query the OrderID, CustomerID and ShipCountry information of the OrderID, CustomerID and ShipCountry of which the order table in the Northwind database does not start with A, B, C, D, E, F and whose last letter
--the letter is "a".
USE Northwind
GO
SELECT OrderID,CustomerID,ShipCountry 
FROM orders 
WHERE ShipCountry LIKE '[^ABCDEF]%a'

--8. Query the average, highest and lowest grades of all students who have taken course C02.
SELECT AVG(Grade) average, MAX(Grade) highest,



--9. Count the number of students in each department.
SELECT Sdept department, COUNT(*) number of people
FROM student 
WHERE Sdept IN(SELECT DISTINCT Sdept FROM student) 
GROUP BY Sdept --10


. Count the number of students taking each course and the highest test score.
SELECT Cname course name, COUNT(*) course number, MAX(Grade) highest test score
FROM SC, course
WHERE SC.Cno IN(SELECT DISTINCT Cno FROM SC ) AND course.Cno=SC.Cno
GROUP BY course.Cname

- -11. Count the number of courses selected by each student, and display the results in increasing order of the number of courses selected.
SELECT student.Sname student name, student.Sno student number, COUNT(SC.Sno) number of courses selected
FROM student 
inner join SC ON SC.Sno=student.Sno
GROUP BY student.Sname,student.Sno
ORDER BY COUNT(SC .Sno) ASC

--12. Count the total number of students taking elective courses and average test scores.
SELECT COUNT(DISTINCT(Sno)) AS total number of students, AVG(Grade) AS average grade
FROM SC --//Use DISTINCT to eliminate duplicate rows

--13. Query the average grades and the number of selected courses for students who have taken more than 2 courses.
SELECT Student.Sname,AVG(Sc.Grade) average grade,COUNT(SC.Sno) number of courses selected
FROM SC  --inner
join join or inner join, inner join is a comparison operator, only returns eligible rows
    JOIN Student ON (SC.Sno = Student.Sno)
    JOIN Course ON (SC.Cno = Course.Cno)
GROUP BY Student.Sname
HAVING COUNT(distinct Course.Cno) >2--group condition

--14. List the total score over 200 Students with grades are required to list the student number and total grade.
SELECT Sno student number, SUM(Grade)
FROM SC
GROUP BY Sno
HAVING SUM(Grade)>200

--15. Query the types of books with an average price of more than 12.0 yuan in the titles table of the pubs database (Type) ,
-- average price and maximum price.
USE pubs
GO
SELECT Type AS book type, AVG(price) AS average price, MAX(price) AS highest price
FROM titles
GROUP BY Type
HAVING AVG(price)>12.0

--16. Query the total price of books with more than 3 books in each category in the titles table of the pubs database.
USE pubs
GO
SELECT Book type=Type, number of books=count(Type), total price of books=SUM(price)
FROM titles
GROUP BY Type
HAVING count(Type)>3

--17. Query the number of courses that have taken course c02 The student's name and department.
SELECT Sname student name, Sdept department, SC.Cno AS elective course
FROM student
inner join SC ON student.Sno=SC.Sno
WHERE SC.Cno='C02' --18

. Query the names of students with scores above 80, Course number and grade, and sort the results in descending order of grade.
SELECT Sname student name, SC.Cno course number, SC.Grade grade
FROM student
inner join SC ON student.Sno=SC.Sno--inner join table SC query
WHERE SC.Grade>80
ORDER BY SC.Grade DESC

--19 . Inquire about the names, genders, and grades of students who have completed the "Basic Database" in the Department of Computer Science.
SELECT Sname name, Ssex gender, SC.Grade grade
FROM student
inner join SC ON Cno IN(SELECT Cno FROM course WHERE Cname='database foundation') -- conditions for displaying grades
AND student.Sno=SC.Sno -- student number of students showing grades
WHERE Sdept='computer department' AND Ssex='Male' --20 

. Query which students are the same age, and ask to list the names and ages of students with the same age.
--This question uses the self-join of the table, so you need to take two aliases for the table, such as A and B
SELECT A.Sname The name of the student of the same age, A.Sage age
FROM student A 
inner join student B ON A.Sage IN(SELECT Sage FROM student WHERE A.Sage=B.Sage AND A.Sname!=B.Sname)
GROUP BY A.Sname,A.Sage
ORDER BY A.Sage --21



. Query which courses have no candidates, and require columns Enter the course number and course name.
SELECT Cno AS course number, Cname AS course name
FROM course
WHERE Cno NOT IN(SELECT DISTINCT SC.Cno FROM SC) --22


. Query the names, course names and test scores of all students with test scores
--requires to query The results are placed in a new permanent table (assuming the new table is named new-sc).
SELECT student.Sname AS The name of the student who has the test score, course.Cname AS course name, SC.Grade AS test score
INTO [new_sc] -- put the query result into the new table new_sc
FROM student,course,SC
WHERE SC .Grade IS NOT NULL AND student.Sno=SC.Sno AND course.Cno=SC.Cno


--23. Query the names, genders, course names, and grades of students in the Department of Information and Computer Science, respectively,
--and The two query results are required to be combined into a single result set
-- and the columns are displayed in the order of department name, name, gender, course name, and course grade.
--//This question uses and union query
SELECT Sdept department name, Sname name, Ssex gender, course.Cname course name, SC.Grade course grade
FROM student 
inner join SC ON student.Sno=SC.Sno
inner join course ON course.Cno=SC.Cno
WHERE Sdept='Information Department'
UNION 
SELECT Sdept name, Sname name, Ssex gender, course.Cname course name, SC.Grade course grade
FROM student 
inner join SC ON student.Sno =SC.Sno
inner join course ON course.Cno=SC.Cno
WHERE Sdept='Computer Department'

or

select sdept, sname,ssex,cname,grade from student,sc,
wherecourse student.sno=sc.sno and sc.cno=course.cno and (sdept='Information Department' or sdept='Computer Department')
?k78771625

-- 24. Use sub-query to realize the following query:
--(1) Query the names and departments of students who have taken course C01. SELECT Sname
AS is the name of the student who has taken course C01, and the department where Sdept AS is
located . number, name. SELECT Sno AS The student number of the student with a score of 80 or above in the Mathematics Department, Sname AS Name FROM student WHERE Sno IN(SELECT Sno FROM SC WHERE Grade>80)  AND Sno IN(SELECT Sno FROM student WHERE Sdept='Math Department') - -(3) Query the name of the course selected by the students of the Department of Computer Science. SELECT Cname AS The name of the course selected by the students of the Department of Computer Science FROM course WHERE 












course.Cno IN(SELECT DISTINCT Cno FROM SC WHERE SC.Sno IN(SELECT Sno FROM student WHERE Sdept='Computer Department')) --25

. Insert the course status of students with a score higher than 80 in the computer department into another In a table, it can be realized in two cases: /////////////?????/
--(1) Create a table in the process of inserting data.
--The method of using SELECT INTO to insert data is to create a new table
SELECT student.Sname AS student name, course.Cname AS elective course, SC.Cno AS course number
INTO [SC_Info1]-- The course status of students with grades higher than 80 is inserted into table SC_Info1.
FROM student,course,SC
WHERE student.Sdept='Computer Department' AND SC.Grade>80 AND course.Cno=SC.Cno AND student.Sno= SC.Sno

--(2) Create a new table first, and then insert data.
--Create table SC_Info2
CREATE TABLE SC_Info2
(
    Sname char(7),
    Cname char(20),
    Cno char(10)
)

--Insert query result into table SC_info2
INSERT SC_Info2
SELECT student.Sname AS student name,course.Cname AS elective course,SC.Cno AS course number
FROM student,course,SC
WHERE student.Sdept='Computer Department' AND SC.Grade>80 AND course.Cno=SC.Cno AND student.Sno=SC.Sno --26.Delete


the course records of students whose course grades are less than 50.
DELETE SC WHERE Grade<50 OR Grade IS NULL

--27.Delete all the students who took the "c01" course. Score plus 10 points.
UPDATE SC 
SET Grade=Grade+10
WHERE Cno='C01'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642081&siteId=291194637