Fifth week of database operations
note
MySQL database names, table names, column names, aliases capitalization rule is this:
1, the database name and table name is strictly case-sensitive;
2, the alias table is strictly case-sensitive;
3, column names and column aliases in all cases are case-insensitive;
4, field content by default is not case sensitive.
First built in the table, this is the preparatory work of our inquiry
Student Table
Create a table
create table student(
Sno int,
Sname varchar(30),
Ssex varchar(10),
Sage int,
Sdept varchar(20));
Insert data
insert into student values("2012151121","李勇","男",20,"CS");
insert into student values("2012151122","刘晨","女",19,"CS");
insert into student values("2012151123","王敏","女",18,"MA");
insert into student values("2012151125","张立","男",19,"IS");
Course Table
Create a table
create table course(
cno int,
cname varchar(30),
cpno int,
ccredit int));
Insert data
insert into Course values("1","数据库",5,4);
insert into Course values("2","数学",NULL,2);
insert into Course values("3","信息系统",1,4);
insert into Course values("4","操作系统",6,3);
insert into Course values("5","数据结构",7,4);
insert into Course values("6","数据处理",NULL,2);
insert into Course values("7","PASCAL语言",6,4);
SC table
To build the table
Create table Sc(
sno int,
cno int,
grade int);
Insert data
insert into SC values(201215121,1,92);
insert into SC values(201215121,2,85);
insert into SC values(201215121,3,88);
insert into SC values(201215122,2,90);
insert into SC values(201215122,3,80);
check sentence
1. Query detailed records of all students
select * from student;
2. Query all students learn numbers and names
select Sno,Sname from student;
3. Query all students learn numbers and names, column aliases to use the column headings to change the results of the query, the column name to the Chinese character "student number" "Student Name." (OPTIONAL)
select Sno as "学生编号",Sname as "学生姓名" from student;
4. Query elective courses student number. (Do not remove duplicate student number and have to re-try)
select distinct Sno from SC;
select Sno from SC;
5. Query 'CS' Department list of all students
select * from student where Sdept="CS";
6. Elective query '1' number of classes
select * from SC where Cno=1;
7. Query boys school number and name
select Sno,Sname from student where Ssex="男";
8. queries test scores have failed the course number of the course. (Do not remove duplicate student number and have to re-try)
select Cno from sc where grade<60;
select distinct Cno from sc where grade<60;
9. query results in 95 to 99 minutes (including 95 points and 99 points) enrollment records between the student number, course number and achievements.
select * from SC where grade>=95 and grade<=99;
select * from SC where grade in (95,99);
10. Query results are not student number, course number and grades between 95 to 99 minutes.
select * from SC where grade not in (95,99);
11. Queries age was 18 years, 20-year-old or 24-year-old student's name and gender. (Several written?)
select Sname,Ssex from student where Sage=18 or Sage=20 or Sage=24;
select Sname,Ssex from student where Sage in(18,20,24);
Queries age of 12. Neither was 18, 20 years old, not a 24-year-old student's name and gender.
select Sname,Ssex from student where Sage not in(18,20,24);
13. The details of the curriculum were the first two words as "according to" the word of course number course, course name and credit.
select Cno,Cname,Ccredit from course where substr(Cname,2,1)="据";
14. Queries course entitled "A_ C" course number and course credits
select Cno,Ccredit,Cname from course where Cname like "%A%C%";
15. The inquiry without first lesson of the course number and course name
select Cno,Cname from course where cpno is NULL;
16. The query is missing the achievements of students in student number and the course number
select Sno,Cno from SC where grade is null;
17. The inquiry boys school number, name, age and location system, the query results by department where the number of lines in descending order, the same lines are arranged by age students in ascending order.
select Sno,Sname,Sage,Sdept from Student where Ssex="男" order by Sdept desc ,Sage asc;
18. The total number of queries courses offered.
select count(*) from Course;
19. Queries students choose courses of gate count
select count(distinct Cno) from SC;
20. queries the youngest of all students.
select min(Sage) from student;
21. queries the youngest male students.
select min(Sage) from student where Ssex="男";
22. Inquiries youngest 'CS' Department of male students.
select min(Sage) from student where Sdept="CS" and Ssex="男";
23. The average score elective query '95001' students.
select avg(grade) from SC where Sno="95001";
24. Query '95001' students elective highest score
select max(grade) from SC where Sno="95001";
25. The number of school students have elective record of inquiry and his corresponding number of elective door.
select Sno,count(Sno) from sc group by Sno;
26 Query students 'MA' information department surnamed Liu 'CS' system or.
select * from student where substr(Sname,1,1)="刘" and Sdept in("CS","MA");