The SQL EXISTS Operator
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT * FROM Suppliers limit 4;
SELECT * FROM Products limit 4;
select * from Suppliers WHERE EXISTS
(SELECT * FROM Products where unitprice >300) limit 4;
SELECT CompanyName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND UnitPrice < 10);
select companyname from suppliers where exists
( select productname from products where unitprice < 10 and
products.supplierid = Suppliers.supplierID)
-------------------
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products limit 4
-> ;
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| 1 | Chai | 1 | 1 | 10 boxes x 20 bags | 18.0000 | 39 | 0 | 10 | 0 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19.0000 | 17 | 40 | 25 | 0 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10.0000 | 13 | 70 | 25 | 0 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22.0000 | 53 | 0 | 0 | 0 |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products order by unitprice limit 4
-> ;
+-----------+----------------------+------------+------------+------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+----------------------+------------+------------+------------------+-----------+--------------+--------------+--------------+--------------+
| 33 | Geitost | 15 | 4 | 500 g | 2.5000 | 112 | 0 | 20 | 0 |
| 24 | Guaraná Fantástica | 10 | 1 | 12 - 355 ml cans | 4.5000 | 20 | 0 | 0 | 1 |
| 13 | Konbu | 6 | 8 | 2 kg box | 6.0000 | 24 | 0 | 5 | 0 |
| 52 | Filo Mix | 24 | 5 | 16 - 2 kg boxes | 7.0000 | 38 | 0 | 25 | 0 |
+-----------+----------------------+------------+------------+------------------+-----------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products order by unitprice desc limit 4;
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
| 38 | C├┤te de Blaye | 18 | 1 | 12 - 75 cl bottles | 263.5000 | 17 | 0 | 15 | 0 |
| 29 | Th├╝ringer Rostbratwurst | 12 | 6 | 50 bags x 30 sausgs. | 123.7900 | 0 | 0 | 0 | 1 |
| 9 | Mishi Kobe Niku | 4 | 6 | 18 - 500 g pkgs. | 97.0000 | 29 | 0 | 0 | 1 |
| 20 | Sir Rodney's Marmalade | 8 | 3 | 30 gift boxes | 81.0000 | 40 | 0 | 0 | 0 |
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products where unitprice >100 order by unitprice desc limit 4;
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
| 38 | C├┤te de Blaye | 18 | 1 | 12 - 75 cl bottles | 263.5000 | 17 | 0 | 15 | 0 |
| 29 | Th├╝ringer Rostbratwurst | 12 | 6 | 50 bags x 30 sausgs. | 123.7900 | 0 | 0 | 0 | 1 |
+-----------+--------------------------+------------+------------+----------------------+-----------+--------------+--------------+--------------+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM Products where unitprice >200 order by unitprice desc limit 4;
+-----------+----------------+------------+------------+--------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+----------------+------------+------------+--------------------+-----------+--------------+--------------+--------------+--------------+
| 38 | C├┤te de Blaye | 18 | 1 | 12 - 75 cl bottles | 263.5000 | 17 | 0 | 15 | 0 |
+-----------+----------------+------------+------------+--------------------+-----------+--------------+--------------+--------------+--------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM Products where unitprice >300 order by unitprice desc limit 4;
Empty set (0.00 sec)
mysql>
mysql> select * from Suppliers WHERE EXISTS
-> (SELECT * FROM Products where unitprice >300);
Empty set (0.01 sec)
mysql> select * from Suppliers WHERE EXISTS
-> (SELECT * FROM Products where unitprice >300) limit 4;
Empty set (0.00 sec)
mysql> select * from Suppliers WHERE EXISTS
-> (SELECT * FROM Products where unitprice >200) limit 4;
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| SupplierID | CompanyName | ContactName | ContactTitle | Address | City | Region | PostalCode | Country | Phone | Fax | HomePage |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| 1 | Exotic Liquids | Charlotte Cooper | Purchasing Manager | 49 Gilbert St. | London | NULL | EC1 4SD | UK | (171) 555-2222 | NULL | NULL |
| 2 | New Orleans Cajun Delights | Shelley Burke | Order Administrator | P.O. Box 78934 | New Orleans | LA | 70117 | USA | (100) 555-4822 | NULL | #CAJUN.HTM# |
| 3 | Grandma Kelly's Homestead | Regina Murphy | Sales Representative | 707 Oxford Rd. | Ann Arbor | MI | 48104 | USA | (313) 555-5735 | (313) 555-3349 | NULL |
| 4 | Tokyo Traders | Yoshi Nagase | Marketing Manager | 9-8 Sekimai
Musashino-shi | Tokyo | NULL | 100 | Japan | (03) 3555-5011 | NULL | NULL |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Suppliers limit 4;
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| SupplierID | CompanyName | ContactName | ContactTitle | Address | City | Region | PostalCode | Country | Phone | Fax | HomePage |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| 1 | Exotic Liquids | Charlotte Cooper | Purchasing Manager | 49 Gilbert St. | London | NULL | EC1 4SD | UK | (171) 555-2222 | NULL | NULL |
| 2 | New Orleans Cajun Delights | Shelley Burke | Order Administrator | P.O. Box 78934 | New Orleans | LA | 70117 | USA | (100) 555-4822 | NULL | #CAJUN.HTM# |
| 3 | Grandma Kelly's Homestead | Regina Murphy | Sales Representative | 707 Oxford Rd. | Ann Arbor | MI | 48104 | USA | (313) 555-5735 | (313) 555-3349 | NULL |
| 4 | Tokyo Traders | Yoshi Nagase | Marketing Manager | 9-8 Sekimai
Musashino-shi | Tokyo | NULL | 100 | Japan | (03) 3555-5011 | NULL | NULL |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products limit 4;
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| 1 | Chai | 1 | 1 | 10 boxes x 20 bags | 18.0000 | 39 | 0 | 10 | 0 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19.0000 | 17 | 40 | 25 | 0 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10.0000 | 13 | 70 | 25 | 0 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22.0000 | 53 | 0 | 0 | 0 |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT CompanyName
-> FROM Suppliers
-> WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 10);
ERROR 1054 (42S22): Unknown column 'Price' in 'where clause'
mysql> SELECT CompanyName
-> FROM Suppliers
-> WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND UnitPrice < 10);
+-------------------------------------+
| CompanyName |
+-------------------------------------+
| Mayumi's |
| Specialty Biscuits, Ltd. |
| PB Kn├ñckebr├Âd AB |
| Refrescos Americanas LTDA |
| Plutzer Lebensmittelgroßmärkte AG |
| Norske Meierier |
| New England Seafood Cannery |
| Lyngbysild |
| Zaanse Snoepfabriek |
| G'day, Mate |
| Ma Maison |
+-------------------------------------+
11 rows in set (0.00 sec)
mysql> SELECT * FROM Suppliers limit 4;
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| SupplierID | CompanyName | ContactName | ContactTitle | Address | City | Region | PostalCode | Country | Phone | Fax | HomePage |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
| 1 | Exotic Liquids | Charlotte Cooper | Purchasing Manager | 49 Gilbert St. | London | NULL | EC1 4SD | UK | (171) 555-2222 | NULL | NULL |
| 2 | New Orleans Cajun Delights | Shelley Burke | Order Administrator | P.O. Box 78934 | New Orleans | LA | 70117 | USA | (100) 555-4822 | NULL | #CAJUN.HTM# |
| 3 | Grandma Kelly's Homestead | Regina Murphy | Sales Representative | 707 Oxford Rd. | Ann Arbor | MI | 48104 | USA | (313) 555-5735 | (313) 555-3349 | NULL |
| 4 | Tokyo Traders | Yoshi Nagase | Marketing Manager | 9-8 Sekimai
Musashino-shi | Tokyo | NULL | 100 | Japan | (03) 3555-5011 | NULL | NULL |
+------------+----------------------------+------------------+----------------------+----------------------------+-------------+--------+------------+---------+----------------+----------------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM Products limit 4;
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| ProductID | ProductName | SupplierID | CategoryID | QuantityPerUnit | UnitPrice | UnitsInStock | UnitsOnOrder | ReorderLevel | Discontinued |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
| 1 | Chai | 1 | 1 | 10 boxes x 20 bags | 18.0000 | 39 | 0 | 10 | 0 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19.0000 | 17 | 40 | 25 | 0 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10.0000 | 13 | 70 | 25 | 0 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22.0000 | 53 | 0 | 0 | 0 |
+-----------+------------------------------+------------+------------+---------------------+-----------+--------------+--------------+--------------+--------------+
4 rows in set (0.00 sec)
mysql> select company name from suppliers where exists
-> ( select priductname from products where unitprice < 11 and
-> products.supplierid = Suppliers.supplierID);
ERROR 1054 (42S22): Unknown column 'company' in 'field list'
mysql> select companyname from suppliers where exists
-> ( select priductname from products where unitprice < 11 and
-> products.supplierid = Suppliers.supplierID)
-> ;
ERROR 1054 (42S22): Unknown column 'priductname' in 'field list'
mysql>
mysql> select companyname from suppliers where exists
-> ( select productname from products where unitprice < 11 and
-> products.supplierid = Suppliers.supplierID);
+-------------------------------------+
| companyname |
+-------------------------------------+
| Exotic Liquids |
| Tokyo Traders |
| Mayumi's |
| Specialty Biscuits, Ltd. |
| PB Kn├ñckebr├Âd AB |
| Refrescos Americanas LTDA |
| Plutzer Lebensmittelgroßmärkte AG |
| Norske Meierier |
| New England Seafood Cannery |
| Lyngbysild |
| Zaanse Snoepfabriek |
| G'day, Mate |
| Ma Maison |
+-------------------------------------+
13 rows in set (0.00 sec)
mysql> select companyname from suppliers where exists
-> ( select productname from products where unitprice < 10 and
-> products.supplierid = Suppliers.supplierID);
+-------------------------------------+
| companyname |
+-------------------------------------+
| Mayumi's |
| Specialty Biscuits, Ltd. |
| PB Kn├ñckebr├Âd AB |
| Refrescos Americanas LTDA |
| Plutzer Lebensmittelgroßmärkte AG |
| Norske Meierier |
| New England Seafood Cannery |
| Lyngbysild |
| Zaanse Snoepfabriek |
| G'day, Mate |
| Ma Maison |
+-------------------------------------+
11 rows in set (0.00 sec)
mysql>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.