Basic Database Operations, databases

Source: Internet
Author: User

Basic Database Operations, databases
I. Data Table Student (S #, Sname, Sage, Ssex) Student table S #: Student ID; Sname: Student name; Sage: Student age; Ssex: Student gender
Course (C #, Cname, T #) curriculum C #, Course No.; Cname: Course name; T #: Instructor No.
SC (S #, C #, score) Student table S #: Student ID; C #, course number; score: score
Teacher (T #, Tname) Instructor table T #: Instructor ID; Tname: Instructor name

Ii. Basic operations:
1. query the student ID of all students whose score is higher than that of the "002" course;

      select a.S# from (select s#,score from SC where C#='001') a,(select s#,score      from SC where C#='002') b      where a.score>b.score and a.s#=b.s#;
2. query the student ID and average score of students whose average score is greater than 60;
      select S#,avg(score)      from sc      group by S# having avg(score) >60;
3. query the student ID, name, number of course selections, and total score of all students;
      select Student.S#,Student.Sname,count(SC.C#),sum(score)      from Student left Outer join SC on Student.S#=SC.S#      group by Student.S#,Sname
4. query the number of teachers surnamed "Li;
Select count (distinct (Tname) from Teacher where Tname like 'Lee % ';
5. query the student ID and name of the student who has not learned the course "ye ping;
Select Student. S #, Student. sname from Student where S # not in (select distinct (SC. S #) from SC, Course, Teacher where SC. C # = Course. C # and Teacher. T # = Course. T # and Teacher. tname = 'peiping ');
6. query the student ID and name of the student who has learned the course 001 and has also learned the course 002;
      select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');
7. query the student ID and name of all students who have learned the course taught by instructor ye Ping;
Select S #, Sname from Student where S # in (select S # from SC, Course, Teacher where SC. C # = Course. C # and Teacher. T # = Course. T # and Teacher. tname = 'peiping 'group by S # having count (SC. C #) = (select count (C #) from Course, Teacher where Teacher. T # = Course. T # and Tname = 'peiping '));
8. query the student ID and name of all students whose score is smaller than the score of course no. "002" than the course No. "001;
      Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2      from Student,SC where Student.S#=SC.S# and C#='001') S_2 where score2 <score;
9. query the student ID and name of all students whose course scores are less than 60;
      select S#,Sname      from Student      where S# not in (select Student.S# from Student,SC where S.S#=SC.S# and score>60);
10. query the student ID and name of the student who has not completed all the courses;
       select Student.S#,Student.Sname       from Student,SC       where Student.S#=SC.S# group by  Student.S#,Student.Sname having count(C#) <(select count(C#) from Course);

11. query the student ID and name of at least one course with the student ID "1001;

       select S#,Sname from Student,SC where Student.S#=SC.S# and C# in select C# from SC where S#='1001';
12. query the student ID and name of at least one student whose student ID is 001;
       select distinct SC.S#,Sname       from Student,SC       where Student.S#=SC.S# and C# in (select C# from SC where S#='001');
13. Change the average score of the Course taught by instructor ye Ping in the SC table;
Update SC set score = (select avg (SC _2.score) from SC SC _2 where SC _2.C # = SC. C #) from Course, Teacher where Course. C # = SC. C # and Course. T # = Teacher. T # and Teacher. tname = 'peiping ');
14. query the student ID and name of other students whose courses are identical to those of "1002;
       select S# from SC where C# in (select C# from SC where S#='1002')       group by S# having count(*)=(select count(*) from SC where S#='1002');
15. Delete the SC table record for learning "ye ping;
Delect SC from course, Teacher where Course. C # = SC. C # and Course. T # = Teacher. T # and Tname = 'peiping ';
16. insert some records into the SC table. These records must meet the following requirements: average score of students whose student ID is no "003;
       Insert SC select S#,'002',(Select avg(score)       from SC where C#='002') from Student where S# not in (Select S# from SC where C#='002');
17. Show the "Database", "enterprise management", and "English" course scores of all students based on average scores in the following format: Student ID ,, database, enterprise management, English, number of valid courses, average effective score
Select s # as student ID, (SELECT score from SC where SC. S # = t. S # and c # = '004 ') AS database, (SELECT score from SC where SC. S # = t. S # and c # = '001') AS enterprise management, (SELECT score from SC where SC. S # = t. S # and c # = '006 ') AS English, COUNT (*) AS number of valid courses, AVG (t. score) AS average score from SC AS t GROUP BY S # ORDER BY avg (t. score)
18. query the highest score and lowest score of each subject: displayed as follows: course ID, highest score, lowest score
Select l. C # As course ID, L. score AS highest score, R. score AS distinct from SC l, SC AS R WHERE L. C # = R.C # and L. score = (select max (IL. score) from SC AS IL, Student AS IM WHERE L. C # = IL. C # and IM. S # = IL. S # group by il. C #) and r. score = (select min (IR. score) from SC AS IR WHERE R.C # = IR. C # group by ir. C #);
19. The average score of each subject ranges from high to high and the pass rate.
SELECT t. C # AS course number, max (course. cname) AS course name, isnull (AVG (score), 0) AS average score, 100 * SUM (case when isnull (score, 0)> = 60 THEN 1 ELSE 0 END) /COUNT (*) AS pass percentage from SC T, Course where t. C # = course. C # group by t. C # order by 100 * SUM (case when isnull (score, 0)> = 60 THEN 1 ELSE 0 END)/COUNT (*) DESC
20. query the average score and pass percentage of the following courses (shown in "1 line"): Enterprise Management (001), Marx (002), OO & UML (003 ), database (004)
Select sum (case when c # = '001' THEN score ELSE 0 END)/SUM (case c # WHEN '001' THEN 1 ELSE 0 END) AS average score of enterprise management, 100 * SUM (case when c # = '001' AND score> = 60 THEN 1 ELSE 0 END)/SUM (case when c # = '001' THEN 1 ELSE 0 END) AS enterprise management pass percentage, SUM (case when c # = '002' THEN score ELSE 0 END)/SUM (case c # WHEN '002' THEN 1 ELSE 0 END) AS Marx average score, 100 * SUM (case when c # = '002 'AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '002 'THEN 1 ELSE 0 END) AS Marx pass percentage, SUM (case when c # = '003' THEN score ELSE 0 END) /SUM (case c # WHEN '003 'THEN 1 ELSE 0 END) as uml average score, 100 * SUM (case when c # = '003 'AND score> = 60 THEN 1 ELSE 0 END)/SUM (case when c # = '003' THEN 1 ELSE 0 END) as uml pass percentage, SUM (case when c # = '004 'THEN score ELSE 0 END)/SUM (case c # WHEN '004' THEN 1 ELSE 0 END) average score of AS database, 100 * SUM (case when c # = '004 'AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '004 'THEN 1 ELSE 0 END) AS database pass percentage FROM SC
21. query the average scores of different courses taught by different teachers from high to low.
SELECT max (Z. T #) AS instructor ID, MAX (Z. tname) AS instructor name, C. C # AS course ID, MAX (C. cname) AS Course name, AVG (Score) AS average Score from SC AS T, Course AS C, Teacher AS Z where T.C # = C. C # and C. T # = Z. T # group by c. C # order by avg (Score) DESC
22. query the transcript of the following course scores of 3rd to 6th students: Enterprise Management (001), Marx (002), UML (003), Database (004)
[Student ID], [Student name], enterprise management, Marx, UML, database, average score
Select distinct top 3 SC. S # As Student ID, Student. sname AS student name, T1.score AS enterprise management, T2.score AS Marx, T3.score as uml, T4.score AS database, ISNULL (T1.score, 0) + ISNULL (T2.score, 0) + ISNULL (T3.score, 0) + ISNULL (T4.score, 0) as total score FROM Student, SC left join SC as T1 on SC. S # = T1.S # AND T1.C # = '001' left join SC as T2 on SC. S # = T2.S # AND T2.C # = '002' left join SC as T3 on SC. S # = T3.S # AND T3.C # = '003 'left join SC as T4 on SC. S # = T4.S # AND T4.C # = '004 'WHERE student. S # = SC. S # and ISNULL (T1.score, 0) + ISNULL (T2.score, 0) + ISNULL (T3.score, 0) + ISNULL (T4.score, 0) not in (select distinct top 15 with ties isnull (T1.score, 0) + ISNULL (T2.score, 0) + ISNULL (T3.score, 0) + ISNULL (T4.score, 0) FROM SC left join SC AS T1 ON SC. S # = T1.S # AND T1.C # = 'k1 'left join SC AS T2 ON SC. S # = T2.S # AND T2.C # = 'k2' left join SC AS T3 ON SC. S # = T3.S # AND T3.C # = 'k3' left join SC AS T4 ON SC. S # = T4.S # AND T4.C # = 'k4 'order by isnull (T1.score, 0) + ISNULL (T2.score, 0) + ISNULL (T3.score, 0) + ISNULL (T4.score, 0) DESC );

23. Print the score of each subject in Statistics. Number of students in each score segment: course ID, course name, [100-85], [85-70], [70-60], [<60]

Select SC. C # as course ID, Cname as course name, SUM (case when score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100-85], SUM (case when score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85-70], SUM (case when score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70-60], SUM (case when score <60 THEN 1 ELSE 0 END) AS [60-] from SC, Course where SC. C # = Course. C # group by SC. C #, Cname;

24. query the average score and rankings of Students

SELECT 1 + (select count (distinct average score) FROM (select s #, AVG (score) AS average score from SC GROUP BY S #) AS T1 WHERE average score> T2. average score) as ranking, S # as student ID, average score FROM (select s #, AVG (score) average score from SC GROUP BY S #) AS T2 order by average score desc;
25. query the top three records of each subject's score (excluding the parallel score)
SELECT t1.S # as student ID, t1.C # as course ID, Score as score from SC t1 WHERE score IN (select top 3 score from SC WHERE t1.C # = C # ORDER BY Score DESC) order by t1.C #;
26. query the number of students selected for each course
       select c#,count(S#) from sc group by C#;
27. Check the student ID and name of all students who have selected only one course.
Select SC. S #, Student. sname, count (C #) AS course number from SC, Student where SC. S # = Student. S # group by SC. S #, Student. sname having count (C #) = 1;
28. query the number of boys and girls
Select count (Ssex) as boys from Student group by Ssex having Ssex = 'male'; Select count (Ssex) as girls from Student group by Ssex having Ssex = 'female ';
29. query the student list with the last name "Zhang"
SELECT Sname FROM Student WHERE Sname like 'sheet % ';
30. query the list of same-name students and count the number of students with the same name
      select Sname,count(*) from Student group by Sname having  count(*)>1;;
31. List of students born on April 9, 1981 (note: the type of the Sage column in Student table is datetime)
      select Sname,  CONVERT(char (11),DATEPART(year,Sage)) as age      from student      where  CONVERT(char(11),DATEPART(year,Sage))='1981';
32. query the average scores of each course. The results are sorted in ascending order based on the average scores. The average scores are the same and the course numbers are sorted in descending order.
       Select C#,Avg(score) from SC group by C# order by Avg(score),C# DESC ;
33. query the student ID, name, and average score of all students whose average score is greater than 85
      select Sname,SC.S# ,avg(score)      from Student,SC      where Student.S#=SC.S# group by SC.S#,Sname having    avg(score)>85;
34. query the names and scores of students whose course names are "databases" and whose scores are less than 60.
Select Sname, isnull (score, 0) from Student, SC, Course where SC. S # = Student. S # and SC. C # = Course. C # and Course. cname = 'database' and score <60;
35. query the Course selections of all students;
      SELECT SC.S#,SC.C#,Sname,Cname      FROM SC,Student,Course      where SC.S#=Student.S# and SC.C#=Course.C# ;
36. query the name, course name, and score of any course whose score is over 70;
      SELECT  distinct student.S#,student.Sname,SC.C#,SC.score      FROM student,Sc      WHERE SC.score>=70 AND SC.S#=student.S#;
37. query failed courses and arrange them in ascending order by course number
      select c# from sc where scor e <60 order by C# ;
38. query the student ID and name of the student whose course number is 003 and whose score is higher than 80;
      select SC.S#,Student.Sname from SC,Student where SC.S#=Student.S# and Score>80 and C#='003';
39. Number of students selected for the course
      select count(*) from sc;
40. query the names of the students with the highest scores and their scores of the students who take the courses taught by "ye ping ".
Select Student. sname, score from Student, SC, Course C, Teacher where Student. S # = SC. S # and SC. C # = C. C # and C. T # = Teacher. T # and Teacher. tname = 'peiping 'and SC. score = (select max (score) from SC where C # = C. C #);
41. query each course and the number of optional students
      select count(*) from sc group by C#;
42. query the student's student ID, course number, and student score with the same course score
      select distinct  A.S#,B.score from SC A  ,SC B where A.Score=B.Score and A.C# <>B.C# ;
43. query the first two of the best scores for each course
SELECT t1.S # as student ID, t1.C # as course ID, Score as score from SC t1 WHERE score IN (select top 2 score from SC WHERE t1.C # = C # ORDER BY Score DESC) order by t1.C #;
44. count the number of optional students in each course (only when the number of students exceeds 10 ). The course number and number of electives must be output. The query results are sorted in descending order of the number of students, and the query results are sorted in descending order of the number of students.

Column. If the number of students is the same, the course numbers are listed in ascending order.

Select C # as course number, count (*) as number of people from SC group by C # order by count (*) desc, c #
45. Search for student IDs that take at least two courses
      select  S#       from  sc       group  by  s#      having  count(*)  >  =  2
46. query the course number and name of all optional courses for all students
      select  C#,Cname       from  Course       where  C#  in  (select  c#  from  sc group  by  c#) 
47. query the names of students who have not completed any course taught by instructor ye Ping.

Select Sname from Student where S # not in (select S # from Course, Teacher, SC where Course. T # = Teacher. T # and SC. C # = course. C # and Tname = 'peiping ');

48. query the student ID and average score of two or more failed courses

      select S#,avg(isnull(score,0)) from SC where S# in (select S# from SC where score <60 group by S# having              count(*)>2)group by S#;

49. Search for students whose scores are less than 60 in the "004" course in descending order.

      select S# from SC where C#='004'and score <60 order by score desc;
50. Delete the score of course 001 of "002"
      delete from Sc where S#='002'and C#='001';


Basic SQL database operations

Command Line
1. display the Database List on the current database server: mysql> show databases;
2. CREATE a DATABASE: mysql> create database name;
3. CREATE a data TABLE: mysql> USE Database Name; mysql> create table Name (field name VARCHAR (20); field name CHAR (1 ));
4. delete a DATABASE: mysql> drop database name;
5. delete a data TABLE: mysql> drop table name;
6. Clear the table records: mysql> delete from table name;
7. INSERT records INTO the table: mysql> insert into table name VALUES ("hyq", "M ");
8. UPDATE table data: mysql-> UPDATE table name SET field name 1 = 'A', field name 2 = 'B' WHERE field name 3 = 'C ';
9. load data into a data table in text mode: mysql> load data local infile "d:/mysql.txt" into table name;
10. Import the. SQL FILE command: mysql> USE Database Name; mysql> source d:/mysql. SQL;
11. Run the command line to change the root password: mysql> update mysql. user set password = password ('new password') where user = 'root'; mysql> flush privileges;
12. three methods to change the password: mysql> update user set password = password ('000000') where user = 'joy _ pen'; mysql> flush privileges; mysql> set password for 'joy _ oen' = password ('000000'); mysql> grant usage on *. * to 'joy _ pen' identified by '20140901 ';
1. Create a database
Command: create database <database Name> For example: create a database named xhkdb mysql> create database xhkdb;
2. display all databases
Command: show databases (Note: There is finally s) mysql> show databases;
3. delete a database
Command: drop database <database Name> example: delete a database named xhkdb mysql> drop database xhkdb;
4. Connect to the database
Command: use <Database Name> For example: if the xhkdb Database exists, try to access it: mysql> use xhkdb; on-screen prompt: Database changed
5. mysql> select database () currently selected (connected ();
6. Table information contained in the current database: mysql> show tables; (Note: There is a last s)
Iii. Table operations. A database should be connected before operations
1. Create a table
Command: create table <table Name ...... remaining full text>

You must be able to perform basic operations on the database, including creating a database, adding and deleting tables in the database, and completing the operations.

Different companies use different development platforms with different focuses, so they cannot be generalized. I personally think that the ability to develop databases and code can be seen, but cannot be found.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.