0% found this document useful (0 votes)
17 views30 pages

Seminar 3 - SQL

Query 10 displays the SalesDate as Dates, InvoiceTotal for each invoice in October by: 1. Selecting the SalesInvoiceID as InvoiceID, SalesDate as Dates, SUM of SoldPrice multiplied by Quantity as InvoiceTotal, and MONTH of SalesDate as Month 2. From the Sales_Inventory and Sales tables 3. Where the MONTH of SalesDate is October 4. And the SalesInvoiceID matches between the two tables 5. Grouped by the SalesInvoiceID, SalesDate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views30 pages

Seminar 3 - SQL

Query 10 displays the SalesDate as Dates, InvoiceTotal for each invoice in October by: 1. Selecting the SalesInvoiceID as InvoiceID, SalesDate as Dates, SUM of SoldPrice multiplied by Quantity as InvoiceTotal, and MONTH of SalesDate as Month 2. From the Sales_Inventory and Sales tables 3. Where the MONTH of SalesDate is October 4. And the SalesInvoiceID matches between the two tables 5. Grouped by the SalesInvoiceID, SalesDate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Seminar 3

Users might want specific information


found in a relational database and
not necessary to go through all the
files to get that information.
The answer is Data Query (ask a
question).
Queries
An example of a query might be: List
the invoice numbers, dates, and
salesperson for sales made to Lola
Doyle.

Copyright © 2021 Pearson Education Ltd.


Class Practice
Query
INTRODUCTION TO
SQL
SELECT expressions
BASIC
STRUCTURE
FROM tables

[WHERE conditions]

[ORDER BY expression [ ASC |


DESC ]]
▪ The first clause in any query is the SELECT
clause. The SELECT clause contains either a list
of the columns you want returned from the
query separated by a comma, or the wildcard *.
▪ The wildcard * when used after SELECT means
that all the columns in the table should be
Basic returned and they are presented in the order in
which they are found in the original table.
Terminology ▪ SELECT AS to change the field name (on the
- Select query result)
▪ SELECT DISTINCT to remove duplicate (on the
query result)
▪ Syntax for selecting a field is the table name, a
period, then the field name for example:
Table1.Field1
Basic Terminology
- From
▪ The second clause in the query is
the FROM clause. The FROM clause
indicates against which table to
run the query (Source).
▪ If the source is more than 1 table
can use the comma in between the
tables
Basic Terminology
- Where
▪ The WHERE clause is used to filter
records.
▪ It is used to extract only those
records that fulfill a specified
condition.
▪ It is used to return data that
matches a specified condition and
is combined with the comparison
operators =, !=, >, <, >=, and <=
Basic Terminology –
Order By
▪ The ORDER BY keyword sorts the
records in ascending order by default.
To sort the records in descending
order, use the DESC keyword.
▪ Can add: DESC or ASC
SQL Preparation
Step 1. Open Access.
Step 2. File -> New -> Blank desktop database
Step 3. Label the database: AIS_1_Student ID

• How to get the access license: Access with the


Office 365 education licenses provided by SIT.
Students just have to log into office365.com
with their SIT email to get it
• Upload the first excel file: Customer (Download
from LMS)
Query 01

• Display the CustomerID and CustomerName from the city


of Phoenix

SELECT CustomerID, CustomerName, City


FROM Customer
WHERE City="Phoenix";
Basic Terminology – AND, OR, and NOT
Logical operators SQL uses to combine conditions in WHERE clauses
in order to refine the data returned by a query
• AND is used in the WHERE clause of a query to return all the rows which
meet all of the conditions specified that are joined with AND
• OR returns all the rows in a dataset that meet the first condition specified
before the OR as well as all data that meets the second condition specified
after the OR
• NOT can be used to negate an entire section in the WHERE clause
Query 2

• Display the CustomerID and CustomerName from the city


of Phoenix and Mesa

SELECT CustomerID, CustomerName, City


FROM Customer
WHERE City="Phoenix" OR City="Mesa";
Query 3

• Display the CustomerID and CustomerName from the city


of Snowflake and has Customer ID > 153

SELECT CustomerID, CustomerName, City


FROM Customer
WHERE City="Snowflake" AND CustomerID > 153;
Query 4

• Display the CustomerID and CustomerName from the city


of Snowflake or Phoenix and has Customer ID < 153

SELECT CustomerID, CustomerName, City


FROM Customer
WHERE (City="Snowflake" OR City="Phoenix")
AND CustomerID < 153;
Upload Second
Dataset: Sales
Database Relationship
Database Relationship
Query 5

• List the invoice number(Label it as InvoiceNumber), dates,


and salesperson for sales made to Lola Doyle.
• Before we create the syntax, let’s rewind about the
dataset, where is the location of these fields:
• InvoiceNumber: SELECT CustomerID, CustomerName, City
FROM Customer
WHERE (City="Snowflake" OR City="Phoenix")

• Dates: AND CustomerID < 153;

• Salesperson:
• Lola Doyle = ____________:
Query 5 Trial

SELECT SalesInvoiceID AS InvoiceNumber, SalesDate,


SalesPerson, CustomerName
FROM Sales, Customer
WHERE CustomerName = "Lola Doyle"

Incorrect results because we have not


specified which invoice number that
we want

The want that we want is the list of


invoice that existed on both table AND
under customer name = Lola Doyle
Query 6

• List the SalesDate as Dates, Description, and Quantity for


each transaction in which Washer were sold in October

SELECT Sales.SalesDate AS Dates, Inventory.Description, [Sales_Inventory.Quantity], MONTH(SalesDate) AS Month


FROM Inventory, Sales, Sales_Inventory
WHERE Inventory.Description="Washer"
AND MONTH([Sales.SalesDate]) = 10
AND Sales.SalesInvoiceID=[Sales_Inventory.SalesInvoiceID]
AND [Sales_Inventory.ItemID]=[Inventory.ItemID];
Upload 3rd Datasets
Upload 4th Dataset
Query 7

• Okay, after we tried the Query 6 let’s do simpler query for


additional practice: Display the SalesDate that happened
on the Month of November

SELECT SalesDate, MONTH(SalesDate) AS Month


FROM Sales
WHERE MONTH(SalesDate)=11
Query 8

• The sales manager wants to know how many Fridge were


sold in October
• Label it as: SumOfQuantity
SELECT Sum(Quantity) AS SumOfQuantity, Description, MONTH
Now we are starting to use (SalesDate) AS MONTH
FROM Sales_Inventory, Sales, Inventory
Aggregate functions such WHERE MONTH(Sales.SalesDate)=10
AND Inventory.Description = "Fridge"
as: SUM, MIN, or MAX AND Sales.SalesInvoiceID=[Sales_Inventory.SalesInvoiceID]
AND [Sales_Inventory.ItemID]=[Inventory.ItemID]
GROUP BY MONTH (SalesDate), Description
Query 9

• Query 9 responds to the data request: List Description and


SumOfQuantity of inventory types that sold 2 or more units
in the month of October.

SELECT Description, Sum(Quantity) AS SumOfQuantity


FROM Sales_Inventory, Inventory, Sales
WHERE MONTH(SalesDate) = 10
AND Sales.SalesInvoiceID=[Sales_Inventory.SalesInvoiceID]
AND [Sales_Inventory.ItemID]=[Inventory.ItemID]
GROUP BY Description

HAVING SUM(Quantity)>= 2
The next slide is the
answer (partial) for
this query, but
please, try it first
before you check
for the solution!
Query 9 Answer

• SELECT SUM(Quantity) AS SumOfQuantity,


MONTH(SalesDate) AS Month, Description
• FROM Sales_Inventory, Inventory, Sales
• WHERE MONTH(SalesDate) = 10 AND Description =
"Refrigerator" AND Sales.SalesInvoiceID =
Sales_Inventory.SalesInvoiceID AND Inventory.ItemID =
Sales_Inventory.ItemID
• GROUP BY MONTH(SalesDate), Description
Follow Up Question

• SELECT SUM(Quantity) AS SumOfQuantity,


MONTH(SalesDate) AS Month, Description
• FROM Sales_Inventory, Inventory, Sales
• WHERE Sales.SalesInvoiceID =
Sales_Inventory.SalesInvoiceID AND Inventory.ItemID =
Sales_Inventory.ItemID
• GROUP BY MONTH(SalesDate), Description
Query 10

• Query 10 responds to a data request: List SalesDate as


Dates and InvoiceTotal for each invoice (as InvoiceID) in
October (Display this as Month).
• InvoiceTotal = SoldPrice * Quantity
SELECT Sales.SalesInvoiceID AS InvoiceID, SalesDate AS Dates, SUM(SoldPrice*Quantity) AS
InvoiceTotal, MONTH(SalesDate) AS Month
FROM Sales_Inventory, Sales
WHERE MONTH(SalesDate) = 10
AND Sales.SalesInvoiceID=Sales_Inventory.SalesInvoiceID
GROUP BY Sales.SalesInvoiceID, SalesDate

You might also like