Example Queries Flashcards
Example of average function.
SELECT AVG(column_name) FROM table_name
Example of above average statement.
SELECT ProductName, Price FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products);
Statement that gets count
SELECT COUNT(column_name) FROM table_name;
Example of returns distinct value.
SELECT COUNT(DISTINCT column_name) FROM table_name;
The following SQL statement counts the number of orders from “CustomerID”=7 from the “Orders” table:
SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders WHERE CustomerID=7;
The FIRST() function returns the first value of the selected column.
SELECT FIRST(column_name) FROM table_name;
SQL LAST() Syntax
SELECT LAST(column_name) FROM table_name;
SQL MAX() Syntax
SELECT MAX(column_name) FROM table_name;
SQL MIN() Syntax
SELECT MIN(column_name) FROM table_name;
SQL MIN() Syntax
SELECT MIN(column_name) FROM table_name;
SQL SUM() Syntax
SELECT SUM(column_name
SELECT SUM(column_name) FROM table_name;
SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
The following SQL statement counts as orders grouped by shippers:
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
The UCASE() function converts the value of a field to uppercase.
SELECT UCASE(column_name) FROM table_name;