SQL-based data query

First, the general format of data query:

  

2. Single table query

  1. Query the specified column and the calculated value

    (1) Query the specified column: Just add the field of the specified column to be queried in the SELECT clause. Sometimes, according to the requirements of the question, you must add the DISTINCT keyword before the field.

SELECT DISTINCT  Sno
FROM SC; 

    (2) Query the calculated value: The target column expression in the SELECT clause can be as follows:

  •  arithmetic expressions
  •    string constant
  •    function
  •    column alias
SELECT Sname, 2009 - Sage   -- query arithmetic expression result 
FROM Student;

SELECT Sname, ' Year of Birth: '     -- query string constant 
FROM Student;

SELECT Sname, LOWER (Sdept) --Query     the result of field information processed by the function 
FROM Student;

--The query result is the column information, but the column name on the view is the alias 
SELECT Sname AS NAME, ' Year  of Birth: ' AS BIRTH    
 FROM Student;

  2. Query the tuples that meet the conditions

    (1) Compare size

    Query predicates: =, >, <, >=, <=, !=, <>, !>, !<; NOT + the above comparison operators

SELECT  COUNT (Jno) AS  ' total number of items '  FROM SPJ WHERE Sno =  ' S1 ' ;

    (2) Confirmation range:

    Query predicates: BETWEEN AND, NOT BETWEEN AND

SELECT Sname,Sdept,Sage
FROM Student
WHERE Sage BETWEEN 20 AND 23

    (3) Confirmation set:

    Query predicates: IN , NOT IN    

SELECT Sname,Ssex
FROM  Student
WHERE Sdept IN ( 'IS''MA''CS' );

    (4) Character matching

    Query predicate: [NOT] LIKE '<match string>' [ESCAPE '<escape character>']

    The matching string is a string containing wildcards
    %: represents a string of any length (the length can be 0)
    _: represents a single character

--Inquire the name, student number and gender of all students surnamed Liu. 
SELECT Sname, Sno, Ssex
 FROM Student
 WHERE   Sname LIKE  ' Liu% ' ;
 -- Query the names of students whose surname is "Ouyang" and whose full name is three Chinese characters. 
SELECT Sname
 FROM    Student
 WHERE   Sname LIKE  ' Ouyang_ ' ;

    Use the escape character ESCAPE to escape wildcards to normal characters

--Query the details of courses starting with "DB_" and the third last character is i. 
SELECT   * 
FROM    Course
 WHERE   Cname LIKE   ' DB\_%i_ _ '  
ESCAPE  ' \ ';

    (5) Null value query

    Query predicate: IS NULL or IS NOT NULL

SELECT Sno,Cno
FROM  SC
WHERE  Grade IS NULL

    (6) Multiple condition query

    Query predicates: AND and OR to combine multiple query conditions, wherein the query conditions can contain the above query conditions.

SELECT Sname
FROM  Student
WHERE Sdept= 'CS' AND Sage<20

   3. ORDER BY ... DESC/ASC (default is ascending order)

    Indicates that the query results are displayed in ascending order, ASC is ascending order, and DESC is descending order.

SELECT Sno,Grade
FROM  SC
WHERE  Cno= ' 3 '
ORDER BY Grade DESC

    Notice:

  •     When the sort column contains null values, ASC: tuples with null values ​​in the sort column are displayed last, and DESC: tuples with null values ​​in the sort column are displayed first.
  •     ORDER BY cannot be used in subqueries because ORDER BY sorts the query results.

  4. GROUP BY ... HAVING ...    

    Its function object is the intermediate result table of the query, and the final result table is the grouping result of the query results according to the specified conditions.

    After GROUP BY, it is the grouping basis (field name), which is grouped by the specified value of one or more columns, and the values ​​are equal to one group; after HAVING, most of the aggregation functions meet the conditions, and the aggregation functions will act on the front according to the grouping basis. each group of groups.

/* Query the supplier code that has supplied more than 3 items and the total number of items supplied. */ 
SELECT Sno, COUNT ( DISTINCT Jno) AS  ' total number of items '  
FROM SPJ 
 GROUP  BY Sno 
 HAVING  COUNT ( DISTINCT Jno) >  3 ;

    Notice:

  •  After using the Group By clause, the field column expressions of the Select clause can only be group by and aggregate functions
  •    The main difference between the HAVING phrase and the WHERE clause is that the object of action is different. The WHERE clause acts on the base table or view, and selects tuples that meet the conditions, while the HAVING phrase acts on groups, and selects groups that meet the conditions.

  5. Aggregate functions

  Count:
  COUNT([DISTINCT|ALL] *)
  COUNT([DISTINCT|ALL] <column name>)
  Calculate sum:
  SUM([DISTINCT|ALL] <column name>)
  Calculate average:
  AVG([DISTINCT|ALL] < column name>)
  max and min:
  MAX([DISTINCT|ALL] <column name>)
  MIN([DISTINCT|ALL] <column name>)

SELECT  COUNT (Jno) AS  ' total number of items '  
FROM SPJ 
 WHERE Sno =  ' S1 ' ;

  Note: Can only be used in the SELECT clause or after HAVING.

Second, the connection query

  1. Equivalent and non-equivalent query

  

 

Guess you like

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