|
(a) Single-table query
n querying all records in a table
SELECT * FROM table
n The specified field record in the query table
Select field, field,... from table
n Query Top N records
Select top N percent * from table
n Query with conditions
SELECT * FROM table where condition
N Query removes duplicates
Select distinct field from table
n Queries using aggregate functions (and aliases)
Max () sum () min () Avg () count ()
Select A=b from table
Select B A From table
Select B as a from table
N Fuzzy Query
Like _ single character% one or more characters [] within range [^] not in range
n Sort fields when querying
N Merge Query Results
Union
n query to create a new table
Select field, field into new table name from table
n queries use the Grouping command.
SELECT * FROM Stuinfor
--statistics on the average age of female students
Select AVG (stuage) from stuinfor where ssex= ' female '
--statistics on the average age of male students
Select AVG (stuage) from stuinfor where ssex= ' man '
--statistics on the average age of male and female students
Select Stusex,avg (stuage) from Stuinfor GROUP by Stusex
(b) Multi-table query
N Cross-Connect
SELECT * FROM Stuinfor,cinfor,scinfor
n Connections (correct)
Select field, field from table, table where table. field = table. field
Select field, field from table inner join table on table. field = table. Fields and ...
n OUTER JOIN outer JOIN
L Left Outer connection
Left table full display right table satisfies explicit not satisfied with NULL
L Right Outer connection
(right join) full explicit left table satisfies explicit unsatisfied with null display
L Fully connected
(full join) left + right
(1) Check all records in the student information sheet
SELECT * FROM Stuinfor
(2) Inquiry Student Information Form students ' School number, name, college
Select Sno,sname,scollege from Stuinfor
(3) Check student information sheet for soft or animated college student information
Select * from stuinfor where scollege= ' soft work ' or scollege= ' animation '
SELECT * from Stuinfor where scollege in (' Soft work ', ' animation ')
(4) Check the student information sheet for age in 23-27 student information
Select * from stuinfor where sage between and 27
SELECT * from Stuinfor where sage>=23 and sage<=27
(5) Check the student information sheet for age >28 and soft worker information
Select * from Stuinfor where sage<28 and scollege= ' soft work '
(6) Check the Student Information table for the first five records of the number, name, gender
Select Top 5 sno,sname,ssex from Stuinfor
(7) Inquiry student Information Form which college students come from (several)
Select Scollege from Stuinfor
(select COUNT (distinct scollege) from stuinfor)
(8) The maximum age and minimum age of students in the Student information form
Select Max (Sage) maximum, min (sage) minimum from stuinfor
(9) The total score and the average of the elective course 1th in the Enquiry score table
Select sum (Score), AVG (score) from Scinfor where cno=1
(10) The number of eligible grades in the Enquiry score table
Select count (score) from Scinfor where score>=60
(11) Search students ' information in the Student score table in order from high to low
Select * from Scinfor ORDER BY score Desc
(12) Inquire about student information of surname Li
Select * from stuinfor where sname like '% Li '
(13) The average age of each college is counted separately
Select avg (SAGE) from Stuinfor GROUP by Scollege
(14) Inquiry Student's school number, name, college create a new table N1
Select Sno,sname,scollege to N1 from Stuinfor
(15) Check the student number, name, gender, college and achievement of all students taking the Java course
Select Stuinfor.sno,sname,ssex,scollege,scroe from Stuinfor,cinfor,scinfor
where Scinfor.sno=stuinfor.sno and Scinfor.cno=cinfor.cno and Cname= ' Java '
Select Stuinfor.sno,sname,ssex,scollege,scroe
From stuinfor inner join scinfor on Stuinfor.sno =scinfor.sno
INNER JOIN Cinfor on SCINFOR.CNO=CINFOR.CNO
and Cname= ' Java '
(16) Check the course of all students
SELECT * from stuinfor LEFT join Scinfor on Stuinfor.sno=scinfor.sno
|