Database query examples and SQL statements

Source: Internet
Author: User


Database Query example and SQL statement [SQL] drop database if exists SS; create database SS; www.2cto.com use SS; create table Student (Sno char (9) primary key, sname char (20) unique, Ssex char (2), Sage smallint, Sdept char (20); create table Course (Cno char (4) primary key, cname char (40), Cpno char (4) references Course (Cno), Ccredit smallint); www.2cto.com create table SC (Sno char (9) references Student (Sno), Cno c Har (4) references Course (Cno), Grade smallint, primary key (Sno, Cno); example 1 www.2cto.com: query the names of all students and analysis of their birthdates: the data to be queried is: Sname, the Year of birth (this column is not in the table, but can be calculated) from which tables can obtain the data to be queried: Sname is the attribute of Student, sage is also a property of Student, so the result of the ontology query can be obtained in Student. The date of birth can be queried using the current year minus the age. Statement: select Sname, 20012-Sage from Student ;------------- -------------------------------------------------------------------- Example 2): query the student ID analysis for the course: Data to be queried: Sno analysis: elective course, in other words, it is the Sno corresponding to the ancestor whose course number Cno is not empty. The SC table already contains the Cno information and the student ID, at the same time, you must remove duplicate data because multiple cNOS correspond to which tables of Sno1 can obtain the data to be queried: Sno and Cno are SC attribute columns, therefore, you can obtain the required information query statement in SC: select distinct Sno from SC; example 3): query the name and gender analysis of Students in CS, MA, IS: data to be queried: Sname, S Which tables does sex obtain the data to be queried? Sname and Ssex are the attribute columns of Student. Therefore, the query statement can be: www.2cto.com 1) select Sname in the Student table, ssex from Student where Sdept = 'cs 'or Sdept = 'Ma' or Sdept = 'is'; you can also use the predicate in to find that the attribute belongs to the ancestor of the specified set, so there are queries 2) select Sname, Ssex from Student where Sdept in ('cs ', 'M', 'is'); example 4 ): query the number of course Numbers (Cno) of each course and the number of students corresponding to the course number: Data to be queried: Cno, analysis of the table used for querying the number of course students, Cno And Sno are SC attribute columns: since a Cno can correspond to multiple Sno ,, therefore, you can divide the values with the same Cno into group by, and then calculate each group with count, that is, to calculate the number of rows in the same group, using count (Cno) try the query statement www.2cto.com: grouping first 1): select Cno, Sno from SC group by Cno, Sno/* Note that it cannot be written as select Cno, Sno from SC group by Cno, one principle in group by is that all columns after select must appear after group by */2): select Cno, count (Sno) as num/* count (Sno) is used to calculate the number of Sno or Sno in the same group. */from group by Cn O note that the purpose of group by is to refine the target objects of Aggregate functions. If the query results are not grouped, the aggregate function applies to the entire query result. For example, select count (*) from Student, count (*) calculates the number of all tuples, that is, the total number of students. After grouping, the aggregate function will act on each group, that is, each group has a function value, so the above count (Sno) it is the number of Sno numbers corresponding to a specific value of a when Cno is calculated, rather than the Sno indexes of all query results ----- Example 5 ): query the student ID www.2cto.com who has selected more than two courses. Problem Analysis: The queried data: The table used for Sno query: because the course information is in SC, you only need to use the SC table for analysis: because an Sno can correspond to multiple Cno, you can divide the values with the same Sno into one group by using group by to get the Course Selection for an Sno, then count each group with count (*). Here we use the having phrase to filter items. The query statement is: select Sno from SC group by Sno having count (*)> 2; example 6): query the student's (student ID) Sno. (name) Sname, Cname (Course name), Grade (score) Problem Analysis: query data: tables involved in Sno, Sname, Cname, and Grade: it should be because Sno and Sname are Student attribute columns, Cname is Course attribute columns, and Grade is SC attribute columns. Therefore, Student, Course, and SC are used for analysis: the three tables are the www.2cto.com query statement associated with Sno and Cno: select Student. sno, Sname, Cname, Grade from Student, SC, Course where Student. sno = SC. sno and SC. cno = Course. cno; example 7): query the Problem Analysis of the students in the same department as that of "Liu Chen": query data: Student information. The condition is the same as that of Liu Chen. Unknown data: known Data of the department where Liu Chen is located: The table involved by the Student name Liu Chen: Student analysis: Unknown Data Query statements can be queried based on known data: 1) determine the system where Liu Chen is located select Sdept from student where Sname = "Liu Chen" 2) Find the Student information in the same department as the result of the 1 query select * from student where Sdept = 'cs '; in combination, select * from Student where Sdept in (select Sdept from student where Sname = ""). Because each Student can only have one department, only one query result in 1 is returned, therefore, you can also write the following statement www.2cto.com: select * from Student where Sdept = (select Sdept from student where Sname = ""). In fact, you can also use your own connection to query: think of Student as two tables A and B, then the question can be described as follows: query the Student information of: it is required that the department where A is located be the same as the department where B is named Liu Chen, then, you can write the statement as follows: select * from Student as A where Sdept = (select Sdept from student as B where Sname = ""), and then improve it to select. * from Student A, Student B where. sdept = B. sdept and B. sname = "Liu Chen"; Example 8): Find the course number of each student that exceeds the average score of the course he or she chooses: calculate the Sno and Cno query statements for each student whose score exceeds the average score of the class: select Sno, Cno from SC where Grade> (select AVG (Grade) from SC) all the problems compared with the change, it is easy to write the following statement select Sno, Cno from SC as A where Grade> = (select avg (Grade) from SC as B where. sno = B. sno); www.2cto.com Example 9): Set the score of all students in the computer system to zero. Problem Analysis: involved table: Score Table in SC, Department Sdept in Student table, therefore, two query statements (SC and Student) are involved: update SC SET Grade = 100 WHERE 'cs '= (select Sdept from Student where Student. sno = SC. sno); www.2cto.com query statement 2): update SC set Grade = 0 where SC. sno in (select Student. sno from Student where Sdept = 'cs ');

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.