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/11/02 08:44] – [Unions] 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 = ' | ||
+ | |||
</ | </ | ||
database/mysql.1698914683.txt.gz · Last modified: by skipidar