Week+2SQL
Week+2SQL
WildCards:
Wildcards: allow more precise search
Math Operators.
Filtering
Narrow the data, analysis to get specific about the data we want.
Goals
• Describe the basics of filtering,
• Use WHERE clause with common operators
• Use BETWEEN clause
• Explain concept of NULL Value
Why Filter?
• Reduce the number of data we want to work and retrieve, therefore, increase
speed and performance,
• reduce strain on the client application,
• governance limitation,
To do filtering we use WHERE clause which comes after SELECT and FROM:
IS NULL
If you want to look for something where there is just no information for that
column, that's where you would want to simply use is null.
OR Operator
A database management system will not evaluate the second condition if the first
condition is met.
If you want both conditions to be checked you use AND.
Once it finds a product with name Tofu, it will not return anything with name Konbu
!!!!
SELECT ProductName, unitPrice, supplierId, productId, unitsInStock
FROM Products
WHERE productName = ‘Tofu’ OR ‘Konbu’
t%@gmail.com’
These produce the same result:
WHERE size LIKE ‘%pizza’ and WHERE size LIKE ‘_pizza’
Another wild card is bracket to specify a character in a specific location within a string. [not in
SQLite]
SELECT something
FROM database
ORDERY BY characteristic
Rules:
• Takes name of one or more columns
• Add a comma after each additional column
• Can sort by a column not retrieved
• Must always be the last clause in a SELECT statement
ORDER BY 2, 3
2 means 2nd column and 3 means 3rd column.
If you're using order by descending (DESC) and have unit price, it's not
going to do it for all of the columns after the descending. You have to specify
each individual columns for ascending (ASC) and descending, if you want it
that way.
DESC or ASC only applies to the column names it directly precedes.
Math Operations
SELECT productId,
UnitsOnOrder,
unitPrice,
UnitsOnOrder * unitPrice AS total_order_cost
FROM Products
SELECT productId,
Quantity,
unitPrice,
Discount,
(unitPrice - Discount) * Quantity AS total_cost
FROM orderDetails
Aggregate Functions
Aggregate functions are used to summarize data.
AVERAGE, COUNT, MIN, MAX, SUM
DISTINCT
MAX()
NOTE: Rows containing no or NULL values will be ignored by the AVG() function.
Distinct
SELECT COUNT(DISTINCT CustomerID)
FROM Customers
• If the word DISTINCT is not specified, ALL is assumed! For example, you may have a
customer who's in a table multiple times. If you're simply doing a count on your
customer IDs and you don't distinguish to count just the distinct customer IDs, you
may be getting duplicate records in there.
• Cannot use DISTINCT on COUNT(*)
GROUPING DATA
GROUP BY and HAVING.
We want to know the number of customers we have and we want to know this by each
region.
In this example, we want the count of orders for customers. But we only want to see
the total orders for the customers who've had more than two orders. So to do this,
we're going to select our customer IDs, then we're going to count all of the records as
orders.
Another e.g.
SELECT supplierID, COUNT (*) AS num_prod
FROM Products
WHERE unitPrice>=4
GROUP BY supplierID
HAVING COUNT(*)>=2