8 SQL database job training ELECT (nested query EXISTS, collection, query a derived table based)

EXISTS predicate

  • Existential quantifier

With the predicate EXISTS subquery does not return any data, produced only a logical true value "true" or logically false value "false".
If the inner query result is not empty, then the outer layer of the WHERE clause of the inner query returns a true value if the result is empty, the outer layer of the WHERE clause returns false

EXISTS subquery by the leads, with the goal column expression often used *, because the subquery with EXISTS only return true or false value, given the column name moot.

NOT EXISTS predicate

If the inner query result is not empty, then the outer layer of the WHERE clause returns false
if the inner query result is empty, the outer layer of the WHERE clause returns a true value

[Example 3.60] No. 1 query for all elective courses student names.


SELECT Sname  
FROM Student   
 WHERE EXISTS
(SELECT *
 FROM SC
WHERE Sno=Student.Sno AND Cno= ' 1 ');

[Example 3.61] Query student's name is not No. 1 elective courses.

 SELECT Sname
 FROM     Student
 WHERE NOT EXISTS
 (SELECT *
 FROM SC
 WHERE Sno = Student.Sno AND Cno='1');

[Example 3.62] query enrolled in a full course of student names.

SELECT Sname
FROM Student
WHERE NOT EXISTS
(SELECT *
FROM Course
WHERE NOT EXISTS
(SELECT *
 FROM SC
 WHERE Sno= Student.Sno
  AND Cno= Course.Cno
 )
);
  • With EXISTS / NOT EXISTS implement logical implication

SQL language contains no logical operation can be used to convert the predicate calculus predicate logic implication equivalent to:
Here Insert Picture Description

[Example 3.63] query least number of students enrolled in a full course of 201,215,122 student elective.
NOT EXISTS predicate is represented by:

SELECT DISTINCT Sno
   FROM SC SCX
   WHERE NOT EXISTS
   (SELECT *
   FROM SC SCY
   WHERE SCY.Sno = ' 201215122 '  AND
   NOT EXISTS
   (SELECT *
   FROM SC SCZ
   WHERE SCZ.Sno=SCX.Sno AND
   SCZ.Cno=SCY.Cno));

[Example 3.64] Department of Computer Science student inquiry and no older than 19 year-old student.

SELECT *
FROM Student
WHERE Sdept= 'CS'
UNION
SELECT *
FROM Student
WHERE Sage<=19;

UNION: a plurality of query results when combined, the system automatically remove duplicate tuples UNION ALL: when a plurality of query results combined, repeated reservation tuple

[Example 3.65] query elective courses 1 or 2 of elective courses students.

    SELECT Sno
    FROM SC
    WHERE Cno=' 1 '
    UNION
    SELECT Sno
    FROM SC
    WHERE Cno= ' 2 ';

[Example 3.66] is actually a query students aged not more than 19 years in the Department of Computer Science.

	    SELECT *
    	FROM Student
    	WHERE Sdept= 'CS' AND  Sage<=19;

[Example 3.67] query both elective courses 1 and 2 of elective courses students.

 SELECT Sno
FROM SC
WHERE Cno=' 1 ' 
INTERSECT
SELECT Sno
FROM SC
WHERE Cno='2 ';

[Example 3.67] may be expressed as a nested query:

     SELECT Sno
      FROM    SC
      WHERE Cno=' 1 ' AND Sno IN
      (SELECT Sno
      FROM SC
      WHERE Cno=' 2 ');

[Example 3.68] Department of Computer Science student inquiry and age not more than 19-year-old student set difference.

SELECT *
FROM Student
WHERE Sdept='CS'
EXCEPT
SELECT  *
FROM Student
WHERE Sage <=19;

Subquery not only appear in the WHERE clause,
may also appear in the FROM clause,
then the subquery derived table generated temporary target of the query main query.

[Example 3.57 change] number to identify each student over the course of his own grade point average of elective courses

SELECT Sno, Cno
FROM SC, (SELECT  Sno, Avg(Grade) 
                    FROM   SC 
		           GROUP BY   Sno)
                    AS   Avg_sc(avg_sno,avg_grade)
WHERE SC.Sno = Avg_sc.avg_sno
                    and SC.Grade >=Avg_sc.avg_grade

[Example 60 modified] Find all elective courses No. 1 student's name, you can use the following query is completed:

SELECT Sname
FROM     Student,  
(SELECT Sno FROM SC WHERE Cno=' 1 ') AS SC1
WHERE  Student.Sno=SC1.Sno;

SELECT clause:
DISTINCT: eliminate duplicate row values. If you do not specify DISTINCT keyword, the default is ALL.
Target column expression: the specified attribute columns to display. All queries column
( ). It may be an arithmetic expression, string constants, functions, and so on.
FROM clause: specifying query object.
Based on the derived table query AS
the WHERE clause: Specify the query criteria. Logical operators AND and OR are connected a plurality of query conditions. Size comparison, to determine the scope (BETWEEN AND), determining a set of the IN (), the matching character ([NOT] LIKE).
Join query: WHERE clause conditions are used to connect the two tables, the format: [<table name 1>] <1 column name> <comparison operator> [. <Table name 2>] <2 column name>. FROM own connection must use the "alias."
GROUP BY clause: query results by the specified column packet, equal to the property value tuples as a row group. Typically aggregate function in each role.
HAVING phrase: only meet the specified criteria was set to be output.
Aggregate function can only be used in the SELECT clause and the GROUP BY clause HAVING clause. If no grouping query results, aggregate functions will be applied to the entire query results. After the results of the query packet, the aggregate functions are applied to each group. Specified value or more columns of a packet, a set of values is equal.
WHERE clause HAVING phrase difference: the role of objects of different
WHERE clause applies to the base table or view, which satisfies the condition of selected tuples;
HAVING phrase acting groups, and select the group to meet the conditions.
Nested query: A query block nested in the WHERE or HAVING clause of the phrase. (With IN predicate with a comparison operator, with the sub-queries predicates ANY or ALL)
*

ORDER BY clause: only the final query results by one or more attributes column. The default value of the ASC ASC; descending DESC.

Published 11 original articles · won praise 6 · views 2708

Guess you like

Origin blog.csdn.net/lrx359641708/article/details/105028115