Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language – The Fundamentals

Similar presentations


Presentation on theme: "Structured Query Language – The Fundamentals"— Presentation transcript:

1 Structured Query Language – The Fundamentals
SQL Structured Query Language – The Fundamentals By Hao Nguyen 1

2 Agenda Overview of SQL Joins SQL Functions and Examples
Common Commands Joins SQL Functions and Examples Where In Distinct Like Coalesce Case Count Avg Order Group by

3 SQL – Quick Review SQL is a data manipulation language.
SQL commands are interpreted by the database management system (DBMS) engine. SQL commands can be used interactively as a query language within the DBMS. SQL commands can be embedded within programming languages (e.g, SAS). A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders").  Tables contain records (rows) with data.

4 Most Comman SQL Commands
SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index

5 SQL Joins

6 SQL Joins Cont. SQL joins are used to combine rows from two or more tables, based on a common field between them (Primary Key) Types of SQL Joins INNER JOIN: Returns all rows when there is at least one match in BOTH tables (if a and b) LEFT JOIN: Return all rows from the left table, and the matched rows from the right table RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table FULL JOIN: Return all rows when there is a match in ONE of the tables

7 SQL WHERE Clause Operators in the WHERE Clause Operator Description =
Equal <> Not equal. Note: In some versions of SQL this operator may be written as != > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN To specify multiple possible values for a column

8 SQL CASE – Variable Creation

9 SQL LIKE

10 SQL COALESCE – Variable Creation

11 SQL COUNT – Group by

12 SQL IN

13 SQL DISTINCT - Where

14 SQL WHERE MIN

15 SQL Advance Examples #This query pulls all of a Customer’s Order;
select a.ContactName, a.CustomerId, b.OrderId, sum(d.Price) as OrderTotal from Customers a left join Orders b on a.CustomerId=b.CustomerId left join OrderDetails c on b.OrderId=c.OrderId left join Products d on c.ProductId=d.ProductId where a.customerId=7 group by ContactName,a.CustomerId,b.OrderId *1 *2 *3 *4 *1: SUM function used to get total cost of an order *2: using LEFT JOIN to pull orders by customer, with CustomerID as the joining key *3: WHERE statement filters out results to only CustomerID we want *4: GROUP BY statement groups all results by the Customer and OrderId

16 SQL Advance Examples #query gets average total of Orders ;
select a.ContactName, a.CustomerID, count(b.OrderId) as numOrders, sum(c.OrderTotal) as OrderTotal, sum(c.OrderTotal)/count(c.OrderId) as AverageOrder from Customers a left join Orders b on a.CustomerId=b.CustomerID left join (SELECT a.OrderId, sum(Price) as OrderTotal FROM [OrderDetails] a left join Products b on a.ProductId=b.ProductId group by OrderId) c on b.OrderId=c.OrderId group by a.ContactName,a.CustomerId having AverageOrder not null order by AverageOrder desc; *1 *2 *3 *4 *5

17 SQL Advance Examples *1: COUNT function used to count the total number of orders per customer *2: Average Order Total is calculated by getting the SUM of all orders then dividing by the total number of orders per customer *3: This is called a SUB-SELECT statement. It’s another query that is nested inside our main query, returning a table as a result that is otherwise not available from in our database. We use that result to join with the other tables in the query. *4: HAVING statement must be used along with the GROUP BY statement to filter out anything as a result of the GROUP BY. In this case, we are filtering out customers with zero orders. This could be used for marketing where we only want to target customers who have actually placed orders in the past. *5: ORDER BY statement to order the results in descending order so we view the customers with the highest average total per order.

18 SQL Training


Download ppt "Structured Query Language – The Fundamentals"

Similar presentations


Ads by Google