W3. SQL Exists Flashcards
Q: What does the SQL EXISTS operator do?
A: It tests for the existence of any record in a subquery, returning TRUE if one or more records are found.
Q: When does the EXISTS operator return TRUE?
A: When the subquery returns one or more records.
Q: Write the syntax for using EXISTS.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Q: Write a query to list suppliers with a product price less than 20 using EXISTS.
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.SupplierID AND Price < 20);
Q: Write a query to list suppliers with a product price equal to 22 using EXISTS.
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.SupplierID AND Price = 22);