Question

In: Computer Science

--Know how to nest subqueries: in, not in, some, all, and exists. Know how to nest...

--Know how to nest subqueries: in, not in, some, all, and exists. Know how to nest queries in the from clause. Be able to use the with clause. Be able to use scalar subqueries.
--Know how to use correlation variables in subqueries.
--Know how to use case in queries.
--Know how to insert, delete, and update using subqueries.

Study guide please give examples

Solutions

Expert Solution

Nested subqueries: A subquery can be nested inside other subqueries. SQL has an ability to nest queries within one another. A subquery is a SELECT statement that is nested within another SELECT statement and which return intermediate results. SQL executes innermost subquery first, then next level.

Subqueries IN and NOT IN :

ContactTitle IN ('CEO', 'Owner', 'President')
SELECT CompanyName, ContactName, ContactTitle
FROM Customers
WHERE ContactTitle IN ('CEO', 'Owner', 'President');

Exist and Not Exists:

WHERE EXISTS (sub query)
SELECT SalesOrderID,
       RevisionNumber,
       OrderDate
FROM   Sales.SalesOrderHeader
WHERE  EXISTS (SELECT 1
               FROM   sales.SalesPerson
               WHERE  SalesYTD > 3000000
                      AND SalesOrderHeader.SalesPersonID 
                        = Sales.SalesPerson.BusinessEntityID)

When this SQL executes the following comparisons are made:

  1. The WHERE clause returns all records where the EXISTS clause is TRUE.
  2. The EXIST clause uses a correlated subquery. The outer query is correlated to the inner query by SalesPersonID.
  3. Only SalesPersons with SalesYTD greater than three million are included in the results.
  4. The EXISTS clause returns TRUE if one or more rows are returned by the subquery.

Subqueries with the INSERT Statement:

Subqueries also can be used with INSERT statements. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date or number functions.

The basic syntax is as follows.

INSERT INTO table_name [ (column1 [, column2 ]) ]
   SELECT [ *|column1 [, column2 ]
   FROM table1 [, table2 ]
   [ WHERE VALUE OPERATOR ]

Example

Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table. Now to copy the complete CUSTOMERS table into the CUSTOMERS_BKP table, you can use the following syntax.

SQL> INSERT INTO CUSTOMERS_BKP
   SELECT * FROM CUSTOMERS 
   WHERE ID IN (SELECT ID 
   FROM CUSTOMERS) ;

Subqueries with the UPDATE Statement

The subquery can be used in conjunction with the UPDATE statement. Either single or multiple columns in a table can be updated when using a subquery with the UPDATE statement.

The basic syntax is as follows.

UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
   (SELECT COLUMN_NAME
   FROM TABLE_NAME)
   [ WHERE) ]

Example

Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table. The following example updates SALARY by 0.25 times in the CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.

SQL> UPDATE CUSTOMERS
   SET SALARY = SALARY * 0.25
   WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
      WHERE AGE >= 27 );

Subqueries with the DELETE Statement

The subquery can be used in conjunction with the DELETE statement like with any other statements mentioned above.

The basic syntax is as follows.

DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
   (SELECT COLUMN_NAME
   FROM TABLE_NAME)
   [ WHERE) ]

Example

Assuming, we have a CUSTOMERS_BKP table available which is a backup of the CUSTOMERS table. The following example deletes the records from the CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.

SQL> DELETE FROM CUSTOMERS
   WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
      WHERE AGE >= 27 );

Related Solutions

It shows me that 1 error exists in this code but I didn't know how to...
It shows me that 1 error exists in this code but I didn't know how to fix this error so if you can help I will appreciate it. Language C++. Code: #include <iostream> #include <string> #include <iterator> #include <fstream> #include <sstream> #include <cstdlib> #include <set> using namespace std; class Book { private: string BookId; string BookISBN; string Publisher; int PublisherYear; double Price; int Quantity; string Author; public: string SetBookId(); string SetBookISBN(); string SetPublisher(); int SetPublisherYear(); double SetPrice(); int SetQuantity(); string...
How do we know when social or political inequality exists? What does it mean to be...
How do we know when social or political inequality exists? What does it mean to be “equal”? Are equity and equality the same thing? Should our constitution merely provide the potential for equality to exist? Or should it guarantee an equal outcome?
Snow Geese lay 4 eggs in a nest. A field biologist wanted to know if there...
Snow Geese lay 4 eggs in a nest. A field biologist wanted to know if there is a relationship between the order in which the eggs are laid and the sex of each egg. Please see the table below. Egg order -> 1 2 3 4 Total Male 17 16 7 5 45 Female 10 9 17 14 50 Total 27 25 24 19 95 Is there a relationship between sex and laying order in snow geese? 1. What are...
We all know that there are bound to be some marketing campaigns that will fail. Write...
We all know that there are bound to be some marketing campaigns that will fail. Write about a marketing strategy/campaign that went wrong (think Ford Edsel, New Coke, Netflix/Quikster). Be sure to use a SWOT (Strengths, Weakness, Opportunities and Threats) Analysis to help explain what happened. Explain what the company as trying to market, what was the outcome, why did it fail and if the company survived it.
4. This problem is about some function. All we know about the function is that it...
4. This problem is about some function. All we know about the function is that it exists everywhere and we also know the information given below about the derivative of the function. Answer each of the following questions about this function. Be sure to justify your answers. f ′(−5) = 0 f ′(−2) = 0 f ′(4) = 0 f ′(8) = 0 f ′(x) < 0 on (−5,−2), (−2,4), (8,∞) f ′(x) > 0 on (−∞,−5), (4,8) a. Identify...
How can we be CERTAIN we know anything at all?
How can we be CERTAIN we know anything at all?
2. If an externality exists, it will be experienced by a. some people not directly involved...
2. If an externality exists, it will be experienced by a. some people not directly involved in the production or consumption of the product. b. the consumers of the product only. c. the producers of the product only. d. the government only. 3. A free market fails when a. firms that produce goods which create negative externalities earn high profits. b. firms that produce goods which create positive externalities go bankrupt. c. there is government intervention. d. there is an...
Could you give me some tips on how to know how to identify chiral centers in...
Could you give me some tips on how to know how to identify chiral centers in a large molecule? I know that a chiral center is surrounded by 4 different groups...but I'm finding it a little bit hard to recognize multiple chiral center in large structures.
If you want to know if there is any difference at all in how many people...
If you want to know if there is any difference at all in how many people prefer any of five different salad dressings, you would do what kind of test? Chi square goodness of fit Chi square test of independence Independent sample (simple) ANOVA Repeated measures(matched sample)ANOVA Factorial ANOVA
For a conservative force F, all if the following are correct except A. there exists an...
For a conservative force F, all if the following are correct except A. there exists an associated potential energy function. B. the line integral of F between points A and B by way of point C is independent of point C. C. the line integral of F around a closed path is zero. D. the line integral of F with limits A and B is independent of the order of the limits. E. the force F must be the net...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT