SQL Multi-Table Selects & Subqueries
10 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is a subquery?

A subquery is a SELECT statement that is coded within another SQL statement.

Which of the following clauses can be used within a query that allows a subquery to be introduced?

  • WHERE (correct)
  • HAVING (correct)
  • FROM (correct)
  • SELECT (correct)
  • List the names of all the customers who are in the same city as Freddi using a subquery.

    SELECT customer_id, first_name, state FROM customer WHERE state = (SELECT state FROM customer WHERE first_name = 'Freddi')

    List the product with the highest unit price.

    <p>SELECT product_id, name, unit_price FROM product WHERE unit_price = (SELECT max(unit_price) FROM product)</p> Signup and view all the answers

    List name of customers that have orders with more than 3 quantity, using a nested subquery.

    <p>SELECT customer.first_name, customer.last_name FROM customer WHERE customer_id IN (SELECT customer_id FROM cust_order WHERE order_id IN (SELECT order_id FROM order_item WHERE order_item.quantity &gt; 3))</p> Signup and view all the answers

    Using a subquery, write SQL query to insert a new record into order_item where the order_id is Naseby.

    <p>INSERT INTO order_item(order_id, product_id, quantity, unit_price) VALUES((SELECT order_id FROM cust_order WHERE customer_id = (SELECT customer_id FROM customer WHERE last_name = 'Naseby')), 2, 10, 3)</p> Signup and view all the answers

    Use a subquery to write a SQL query that increases the unit price of products by 10% if they have more than 75 items in stock.

    <p>UPDATE product SET unit_price = unit_price *1.1 WHERE quantity_in_stock IN (SELECT quantity_in_stock FROM product WHERE quantity_in_stock &gt; 75)</p> Signup and view all the answers

    Write a SQL query to delete any products that have not been ordered.

    <p>DELETE FROM product WHERE product_id NOT IN (SELECT product_id FROM order_item)</p> Signup and view all the answers

    Using a subquery in the WHERE clause will include the results in the final result set:

    <p>False</p> Signup and view all the answers

    When using a subquery, the first SELECT statement determines the fields that will be displayed in the final result.

    <p>True</p> Signup and view all the answers

    Study Notes

    Lecture 11: Multi-Table Selects & Intro to Subqueries

    • Lecture is about multi-table selects and subqueries in SQL.
    • The outline includes multi-table selects, subqueries, joins vs subqueries, and additional uses of subqueries.

    Multi Table Select

    • Joining more than 2 tables requires multiple JOIN and ON clauses.
    • Example: Getting customer orders with associated products requires identifying tables (e.g., Customer Orders, Order Items, Product), determining join types, starting with the parent table, joining with an intermediary table (Order Items), and finally joining to the Product table.
    • Direct joins between Product and Customer Orders tables are not sufficient for the complete information. An intermediary table called Order Items is needed.

    Subqueries

    • A subquery is a SELECT statement inside another SQL statement.
    • It can return a single value, a list of values (a single column or a table of values).
    • Subqueries can be used in a variety of places, including the WHERE, HAVING, FROM, and SELECT clauses.
    • Examples: finding customers in the same city as another customer, getting the product with the highest unit price, listing customers with orders having more than 3 items.
    • Nested subqueries can be used for complex queries, where a subquery is used within another subquery in the WHERE clause. For example, finding customers having orders with more than 3 items requires using an inner query within the outer query.
    • The order of the parenthesis matters when using nested subqueries.
    • The field in the inner query must correspond to the field inside the outer query.
    • Table names in subqueries can differ from those in the main query.

    Joins vs Subqueries

    • Subqueries can frequently be rewritten as joins; and vice versa.
    • Using the most readable technique is generally preferred when there is no particular reason to favor one over the other.
    • Joins can be more intuitive as they use existing relationships; however, subqueries can be used to obtain aggregate results from one table.
    • Joins can be memory intensive. In a WHERE clause, the results of a subquery might not be included in the final result.

    Uses of Subqueries

    • Uses of subqueries include inserting, updating, and deleting records in a table.
    • Example: Inserting a new record into an order_item with given parameters such as order ID, product ID, quantity, and price.
    • Example: Updating the unit price of products that have more than 75 items in stock by 10%.
    • Example: Deleting products that are not in order_item table.

    Zoo Database (Example)

    • Finding animals of a specific species.
    • Finding events with prices above or equal to average price.
    • Finding animals fed at 9 AM.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz covers multi-table selects and the introduction to subqueries in SQL. You'll learn about the use of joins, intermediary tables, and how subqueries can enhance data retrieval. Test your understanding of these fundamental concepts in relational databases.

    More Like This

    SQL Subqueries Quiz
    24 questions

    SQL Subqueries Quiz

    BraveChalcedony9426 avatar
    BraveChalcedony9426
    SQL Chapter 7: Coding Subqueries
    34 questions
    Use Quizgecko on...
    Browser
    Browser