SQL: Structured Query Language Write SQL to find all people who are at least 30 years old. or or
    SELECT    ssn
    FROM      Person P             P is an alias of Person for saving typing.
    WHERE     P.age >= 30

If there is no WHERE statement, all tuples are returned.

SELECT    ssn, age
FROM      Person P
Write SQL to find all Memphis people who are at least 30 years old. Write SQL to find names of all people who own a restaurant in Memphis . Write SQL to find names of all people who own a Chinese restaurant in Memphis . Write SQL to find names of all people who own a restaurant using his name. Write SQL to find ssn of all people who own a Chinese restaurant. Write SQL to find ssn of all people who own a Chinese restaurant or a French restaurant. Write SQL to find ssn of all people who own a Chinese restaurant and a French restaurant. Notes:   You can use UNION, INTERSECT, EXCEPT between two SQLs.
            Union Compatible means
SELECT    ssn
FROM      Person
INTERSECT
SELECT    Name
FROM      Owner
Person
 
ssn Name age City State Income
(Monthly)
           
           

Write SQL to find all people who earn less than his age.

SELECT    Name, age - Income           Note: You can return an expression.
FROM      Person
WHERE     Income < age
Write SQL to find for each person, which year he was born.
SELECT    Name, 2001 - age As birthyear
FROM      Person     Note: You can give a name for the expression.
Nested Query:       A WHERE clause can itself contain an SQL query.    The useful place is negation.
SELECT    .....
FROM      .....
WHERE     ..... (SELECT    .....
                 FROM      .....
                 WHERE     .....)
Write SQL to find Name of all people who own a Chinese restaurant. Write SQL to find Name of all people who do not own a Chinese restaurant. Write SQL to find Name of all people who own a Chinese restaurant. Write SQL to find Name of all people who do not own a Chinese restaurant. Note:   EXISTS or NOT EXISTS depends on whether getting returns or not from sub query, it does not matter what to return.

Powerful part of nested query: negation

Write SQL to find Name of all people who own all restaurants.
        There is no direct thinking.  So, you need to find the negation.  That is, find people who do not own all restaurants.
        In another word, find people, there is a restaurant, who do not own.

SELECT   Name
FROM     Person
WHERE    NOT EXISTS (
put in "a query return all restaurants that he does not own"
)
            Key: use set difference to find all restaurants he does not own.
                          Select all restaurant then take away the restaurants that he has.
       That is,      ( SELECT     type, City, State
                       FROM       Restaurant
                       EXCEPT
                       SELECT     type, City, State
                       FROM       Owner
                       WHERE      ssn = Person.ssn )
Write SQL to find all restaurants which John Doe (ssn ="123456789") does not own.
SELECT    *
FROM      Restaurant R
WHERE     NOT EXISTS (SELECT    *
                      FROM      OWNER O
                      WHERE     R.type = O.type
                                AND R.City = O.City
                                AND R.State = O.State
                                AND O.ssn = "123456789")
Notes: