You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQL is pronounced “sequel” (SQL is declarative language)
SQL is used to access & manipulate data in databases
Top SQL DBs are MS SQL Server, Oracle, MySQL, PostgreSQL
Database Definitions
RDBMS (Relational Database Management System), Software that stores and manipulates data arranged in relational database tables.
Table A set of data arranged in columns and rows. The columns represent characteristics of stored data and the rows represent actual data entries.
SQL Commands Categories
Data Query Language (DQL)
SELECT - Retrieve data from table(s)
Data Manipulation Language (DML)
INSERT - Insert data into db table
UPDATE - Update data in db table
DELETE - Delete data from table Data
Definition Language (DDL)
CREATE - Create db object (table, view, etc.)
ALTER - Modify db object (table, view, etc.)
DROP - Delete db object (table, view, etc.)
Data Control Language (DCL)
GRANT - Assign privilege
REVOKE - remove privilege
SQL Data Types
SQL have more than 40 different data types. But these seven are the most important ones:
Integer A whole number without a fractional part. Example: 1, 156, 2012412
Decimal A number with a fractional part. Example:. 3.14, 3.141592654, 961.1241250
Boolean A binary value. It can be either TRUE or FALSE.
Date Example: 2017-12-31
Time Example: 23:59:59
Timestamp The date and the time together. Example: 2017-12-31 23:59:59
Text This is the most general data type. But it can be alphabetical letters only, or a mix of letters and numbers and any other characters. Example: hello, R2D2, Haolia23, 124.765.128.412
How to select data from a table
SELECT [Column List]
FROM [Table Name]
WHERE [Search Condition]
Example:
SELECT FirstName, LastName, OrderDate
FROM Orders
WHERE OrderDate >'10/10/2010'
How to insert data in a table
INSERT INTO [Table Name] ([Column List]) VALUES ([Values])
Example:
INSERT INTO Orders (FirstName, LastName, OrderDate) VALUES ('John', 'Smith', '10/10/2010')
How to update data in a table
UPDATE [Table Name] SET [Column1] = [Value1], [Column2] = [Value2], …
WHERE [Search Condition]
Example:
UPDATE Orders SET FirstName ='John', LastName ='Who'WHERE LastName='Wo'
How to delete data from a table
DELETEFROM [Table Name]
WHERE [Search Condition]
Example:
DELETEFROM Orders
WHERE OrderDate '10/10/2010'
How to group data and use aggregates
SELECT [Column List], [Aggregate Function]([Column Name])
FROM [Table Name]
WHERE [Search Condition]
GROUP BY [Column List]
Example:
SELECT LastName, SUM(OrderValue)
FROM Orders
WHERE OrderDate >'10/10/2010'GROUP BY LastName
How to order data
SELECT [Column List]
FROM [Table Name]
WHERE [Search Condition]
ORDER BY [Column List]
Example:
SELECT FirstName, LastName, OrderDate
FROM Orders
WHERE OrderDate >'10/10/2010'ORDER BY OrderDate
How to select data from more than one table
SELECT [Column List]
FROM [Table1]
JOIN [Table2] ON [Table1].[Column1] = [Table2].[Column1]
Example:
SELECTOrders.LastName, Countries.CountryNameFROM Orders
JOIN Countries ONOrders.CountryID=Countries.ID
Using UNION
SELECT [Column List]
FROM [Table1]
UNIONSELECT [Column List]
FROM [Table2]
Example:
SELECT FirstName, LastName
FROM Orders2010
UNIONSELECT FirstName, LastName
FROM Orders2011
Before diving straight into solution mode for the business requirements, I need to take a look at the data with EDR (Entity-Relationship Diagrams) to identify different data relationships between tables. The EDR of these datasets can be viewed as below:
1. SELECT
Situation
We want to send out a promotional email to our existing customers!
Challenge
Use a SELECT statement to grab the first and last names of every customer and their email address.
Solution
SELECT first_name, last_name, email FROM customer
LIMIT10;
A customer wants to know what the movie “Outlaw Hanky” is about.
Challenge
Could you give them the description for the movie “Outlaw Hanky”?
Solution
SELECT description
FROM film
WHERE title ='Outlaw Hanky'
description
A Thoughtful Story of a Astronaut And a Composer who must Conquer a Dog in The Sahara Desert
5. SELECT WHERE
Situation
A customer is late on their movie return, and we’ve mailed them a letter to their address at ‘259 Ipoh Drive’. We should also call them on the phone to let them know.
Challenge
Can you get the phone number for the customer who lives at ‘259 Ipoh Drive’?
Solution
SELECT phone FROM address
WHERE address ='259 Ipoh Drive'
phone
419009857119
6. ORDER BY and LIMIT
Situation
We want to reward our first 10 paying customers.
Challenge
What are the customer ids of the first 10 customers who created a payment?
Solution
SELECT customer_id
FROM payment
ORDER BY payment_date ASCLIMIT10
customer_id
416
516
239
592
49
264
46
481
139
595
7. ORDER BY and LIMIT
Situation
A customer wants to quickly rent a video to watch over their short lunch break.
Challenge
What are the titles of the 5 shortest (in length of runtime) movies?
Solution
SELECT title,length
FROM film
ORDER BY length ASCLIMIT5
title
length
Labyrinth League
46
Alien Center
46
Iron Moon
46
Kwai Homeward
46
Ridgemont Submarine
46
8. SELECT COUNT
Situation
If the previous customer can watch any movie that is 50 minutes or less in run time, how many options does she have?
Challenge
Use a SELECT COUNT(title) to grab all the title of the movie that is 50 minutes or less in run time.
Solution
SELECTCOUNT(title)
FROM film
WHERE length <=50
count
37
9. HAVING
Challenge
Return the customer IDs of customers who have spent at least $110 with the staff member who has an ID of 2.
Solution
SELECT customer_id, SUM (amount)
FROM payment
WHERE staff_id =2GROUP BY customer_id
HAVINGSUM(amount) >110;
customer_id
SUM
187
110.81
148
110.78
10. LIKE and ILIKE
Challenge
How many films begin with the letter J?
Solution
SELECTCOUNT(title)
FROM film
WHERE title LIKE'J%';
count
20
11. LIKE and ILIKE
Challenge
What customer has the highest customer ID number whose name starts with an 'E' and has an address ID lower than 500?
Solution
SELECT first_name,last_name
FROM customer
WHERE first_name LIKE'E%'AND address_id <500ORDER BY customer_id DESCLIMIT1;
first_name
last_name
Eddie
Tomlin
12. INNER JOIN
Situation
California sales tax laws have changed and we need to alert our customers to this through email.
Challenge
What are the emails of the customers who live in California?
Solution
SELECT district, email
FROM address
INNER JOIN customer ONaddress.address_id=customer.address_idWHERE district ='California';