database:mysql
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
database:mysql [2023/10/30 19:00] – [Joins] skipidar | database:mysql [2023/11/02 11:04] (current) – skipidar | ||
---|---|---|---|
Line 400: | Line 400: | ||
FROM Customers AS c, Orders AS o | FROM Customers AS c, Orders AS o | ||
WHERE c.CustomerName=' | WHERE c.CustomerName=' | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == Having == | ||
+ | |||
+ | |||
+ | The '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | --- The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers | ||
+ | |||
+ | SELECT COUNT(CustomerID), | ||
+ | FROM Customers | ||
+ | GROUP BY Country | ||
+ | HAVING COUNT(CustomerID) > 5; | ||
+ | |||
+ | |||
+ | |||
+ | --- The following SQL statement lists the number of employees having less than 20 sales | ||
+ | SELECT e.EmployeeID, | ||
+ | LEFT JOIN " | ||
+ | ON o.EmployeeID = e.EmployeeID | ||
+ | GROUP BY e.EmployeeID | ||
+ | HAVING COUNT(o.OrderID) < 20 | ||
+ | ORDER BY Sales DESC, FirstName DESC, LastName | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | == Exist == | ||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | --- suppliers with products of price under 20 | ||
+ | SELECT SupplierName | ||
+ | FROM Suppliers | ||
+ | WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20); | ||
+ | |||
+ | |||
+ | |||
+ | --- employees with orders | ||
+ | SELECT LastName | ||
+ | FROM Employees | ||
+ | WHERE NOT EXISTS (SELECT OrderID FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID); | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | == ANY, ALL== | ||
+ | |||
+ | The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values. | ||
+ | |||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | --- product name with productID having quantity=10 somewhere | ||
+ | SELECT ProductName | ||
+ | FROM Products | ||
+ | WHERE ProductID = ANY | ||
+ | (SELECT ProductID | ||
+ | FROM OrderDetails | ||
+ | WHERE Quantity = 10); | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == ANY, ALL== | ||
+ | |||
+ | The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values. | ||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | SELECT OrderID, Quantity, | ||
+ | CASE | ||
+ | WHEN Quantity > 30 THEN 'The quantity is greater than 30' | ||
+ | WHEN Quantity = 30 THEN 'The quantity is 30' | ||
+ | ELSE 'The quantity is under 30' | ||
+ | END AS QuantityText | ||
+ | FROM OrderDetails; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == CASE WHEN == | ||
+ | |||
+ | Case expression | ||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | SELECT OrderID, Quantity, | ||
+ | CASE | ||
+ | WHEN Quantity > 30 THEN 'The quantity is greater than 30' | ||
+ | WHEN Quantity = 30 THEN 'The quantity is 30' | ||
+ | ELSE 'The quantity is under 30' | ||
+ | END AS QuantityText | ||
+ | FROM OrderDetails; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == IFNULL(), ISNULL(), COALESCE(), and NVL() Functions == | ||
+ | |||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | --- fall back to 0 for NULL " | ||
+ | SELECT ProductName, | ||
+ | FROM Products; | ||
+ | |||
+ | |||
+ | --- using boolean ISNULL | ||
+ | SELECT OrderID, | ||
+ | CASE | ||
+ | WHEN ISNULL(AmountDue) THEN "Paid in full" | ||
+ | WHEN DATE(DueDate) < date(NOW()) THEN "Order is past due" | ||
+ | ELSE CONCAT(" | ||
+ | END | ||
+ | FROM OrderDetails; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | == Stored Procedure Example == | ||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | --- procedure | ||
+ | CREATE PROCEDURE SelectAllCustomers | ||
+ | AS | ||
+ | SELECT * FROM Customers | ||
+ | GO; | ||
+ | |||
+ | --- execute | ||
+ | EXEC SelectAllCustomers; | ||
+ | |||
+ | |||
+ | |||
+ | --- procedure with args | ||
+ | CREATE PROCEDURE SelectAllCustomers @City nvarchar(30) | ||
+ | AS | ||
+ | SELECT * FROM Customers WHERE City = @City | ||
+ | GO; | ||
+ | |||
+ | --- exec | ||
+ | EXEC SelectAllCustomers @City = ' | ||
+ | |||
</ | </ | ||
Line 426: | Line 594: | ||
-- join 3 tables together | -- join 3 tables together | ||
+ | --- Achtung - there is only 1 SELECT but multiple joins | ||
SELECT Orders.OrderID, | SELECT Orders.OrderID, | ||
- | FROM ((Orders | + | FROM ((Orders |
- | INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) | + | INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) |
- | INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID); | + | INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID); |
</ | </ | ||
Line 436: | Line 604: | ||
== FULL (OUTER) Join == | == FULL (OUTER) Join == | ||
- | Takes null CustomerIDs from both tables | + | Takes null CustomerIDs from both tables. |
+ | |||
+ | Also shows Customers without Orders. | ||
+ | |||
+ | And Orders without Customers. | ||
<sxh sql> | <sxh sql> | ||
Line 444: | Line 616: | ||
ON c.CustomerID = o.CustomerID | ON c.CustomerID = o.CustomerID | ||
WHERE OrderID > 10441 | WHERE OrderID > 10441 | ||
+ | |||
+ | --- same as FULL OUTER | ||
+ | SELECT * FROM Orders AS o | ||
+ | FULL OUTER JOIN Customers AS c | ||
+ | ON c.CustomerID = o.CustomerID | ||
+ | WHERE OrderID > 10441 | ||
+ | |||
</ | </ | ||
Line 466: | Line 645: | ||
WHERE OrderID > 10441 | WHERE OrderID > 10441 | ||
</ | </ | ||
+ | |||
+ | == Self Join == | ||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | -- show customer pairs from the same city | ||
+ | SELECT A.CustomerName AS CustomerName1, | ||
+ | FROM Customers A, Customers B | ||
+ | WHERE A.CustomerID <> B.CustomerID | ||
+ | AND A.City = B.City | ||
+ | ORDER BY A.City; | ||
+ | |||
+ | -- show the customers, who are in the same city, like " | ||
+ | SELECT A.CustomerName AS CustomerName1, | ||
+ | FROM Customers A, Customers B | ||
+ | WHERE A.CustomerID <> B.CustomerID | ||
+ | AND A.CustomerName = " | ||
+ | AND A.City = B.City | ||
+ | ORDER BY A.City; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==== Unions ==== | ||
+ | |||
+ | == UNION / UNION ALL == | ||
+ | |||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | -- all cities, distinct union | ||
+ | |||
+ | SELECT City FROM Customers | ||
+ | UNION | ||
+ | SELECT City FROM Suppliers | ||
+ | ORDER BY City; | ||
+ | |||
+ | |||
+ | -- UNION ALL means, cities are NOT unique | ||
+ | |||
+ | SELECT City FROM Customers | ||
+ | UNION ALL | ||
+ | SELECT City FROM Suppliers | ||
+ | ORDER BY City; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | When putting together Customer, Supplier you can remember who is who | ||
+ | |||
+ | by adding a **AS Role** column. | ||
+ | |||
+ | It will contain values Customer / Supplier | ||
+ | |||
+ | <sxh sql> | ||
+ | |||
+ | SELECT ' | ||
+ | FROM Customers | ||
+ | UNION | ||
+ | SELECT ' | ||
+ | FROM Suppliers | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
==== Aufgaben ==== | ==== Aufgaben ==== |
database/mysql.1698692413.txt.gz · Last modified: by skipidar