Week 1 Getting Started and Selecting & Retrieving Data With SQL
Week 1 Getting Started and Selecting & Retrieving Data With SQL
Structured Query Language (SQL) is a standard computer language for relational database
management and data manipulation.
Read/retrieve data
Backend Developer
Data Architect
QA Engineer
ETL Developer
Database Admin (DBA)
Data analyst
System Admin
System Engineer
Data Scientist
DBA
Retrieve data;
Data Models
Thinking about your data
Understand the business process or subject matter the data is modeled after
Some concepts
Name Description
Time Types
1960 Hierarchical
1969 Network
Time Types
1970 Relational
1978 Semantic
1985 Object-oriented
Relational Model
Allows for easy querying and data manipulation in an easy, logical and intuitive way. Operational database
Name Description
ER Diagrams
ER Model
Show relationships
Bussiness process
Represented visually
Show links (Primary keys)
1SELECT prod_name
2FROM Products;
1SELECT prod_name
2 ,prod_id
3 ,prod_price
4FROM Products;
1SELECT *
2FROM Products;
Limit results
If the database is very large, we can use LIMIT to limit the number of retrieved results.
1SELECT *
2FROM Products
3LIMIT 5;
Different Syntaxes
Database Codes
1
2|Oracle|```SELECT prod_name FROM products WHERE ROWNUM <=5;```|
3|DB2|```SELECT prod_name FROM products FETCH FIRST 5 ROWS ONLY;`
4## Creating Tables
5```sql
SQLite 6CREATE TABLE Shoes
7(
8Id char(10) PRIMARY KEY,
9Brand char(10) NOT NULL,
10Price decimal(8,2) NOT NULL,
11Desc Varchar(750) NULL
12);
13
14
NULL and Primary Keys
Adding comments
Single line
1SELECT shoe_id
2--,brand_id
3,shoe_name
4FROM Shoes;
Section
1SELECT shoe_id
2/*,brand_id
3,shoe_name
4*/
5FROM Shoes;
WHERE
BETWEEN
IN
OR
NOT
LIKE
ORDER BY
GROUP BY
Wildcards
Math Operators
AVERAGE
COUNT
MAX
MIN
WHERE Clause
1SELECT column_name,column_name
2FROM table_name
3WHERE column_name operator value;
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE ProductName = 'Tofu';
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE UnitPrice >= 75;
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE ProductName <> 'Alice';
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
WHERE UnitsInStock BETWEEN 15 AND
580;
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE ProductName IS NULL;
1SELECT ProductID
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE SupplierID IN (9,10,11);
OR operator
DBMS will not evaluate the second conditions in WHERE clause if the first condition is met.
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE ProductName = 'Tofu' OR 'Konbu';
IN vs. OR
IN works the same as OR.
Benfits of IN:
OR with AND
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE SupplierID = 9 OR SupplierID = 11 AND UnitPrice > 15;
1SELECT ProductName
2,UnitPrice
3,SupplierID
4FROM Products
5WHERE (SupplierID = 9 OR SupplierID = 11) AND UnitPrice > 15;
Don’t rely on the default order, it’s better to have the habit of using the ().
NOT operator
1SELECT *
FROM Employees
2WHERE NOT City = 'London' AND NOT City =
3'Seattle';
Downsides of wildcards
Sorting with ORDER BY
1SELECT something
2FROM database
3ORDER BY characteristic;
Can sort by a column not retrieved
Must always be the last clause in a select statement
1ORDER BY 2,3;
It means data sorted by 2nd and 3rd column.
Sort direction
Math operators
Operator D
+ Addition
- Subtraction
* Multiplication
/ Division
Order of operators
Parenthese
Exponents
Multiplication
Division
Addition
Subtraction
1SELECT ProductID
2,Quantity
3,UnitPrice
4,Discount
5,(UnitPrice - Discount) / Quantity AS Total_Cost
6FROM Orders;
Aggregate Functions
What?
Functions
Average function
Count functions
1SELECT
2Region
3,COUNT(CustomerID) AS total_customers
4FROM Customers
5GROUP BY Region;
Notices
HAVING examples
1SELECT
2CustomerID
3,COUNT(*) AS orders
4FROM Orders
5GROUP BY CustomerID
6HAVING COUNT(*) >=2;
1SELECT
2SupplierID
3,COUNT(*) AS Num_prod
4FROM Products
5WHERE UnitPrice >= 4
6GROUP BY SupplierID
7HAVING COUNT(*) >=2;
Example
1SELECT CustomerID
2,CompanyName
3,Region
4FROM Customers
5WHERE customerID in (SELECT CustomerID
6 FROM Orders
7 WHERE Freight > 100);
Subquery in subquery
1SELECT customer_name
2,customer_state
3,(SELECT COUNT(*) AS orders
4 FROM Orders
5 WHERE Orders.customer_id = Customer.customer_id) AS orders
6FROM Customers
7ORDER BY Customer_name;
Efficient storage
Easier manipulation
Greater scalability
Logically models a process
Tables are related through common values (keys)
Joins
Examples
1SELECT vendor_name
2,unit_price
3,company_name
4FROM suppliers
5CROSS JOIN products;
1SELECT vendor_name
2,product_name
3,product_price
4FROM Vendors,Products
5WHERE Vendors.vendor_id = Products.vendor_id;
Inner Joins
What?
The INNER JOIN keyword selects records that have matching values in both tables.
Example
1SELECT suppliers.CompanyName
2,ProductName
3,UnitPrice
4FROM Suppliers INNER JOIN Products
5ON Suppliers.supplierid = Products.supplierid;
Syntax
1SELECT o.OrderID,
c.CompanyName,
2e.LastName
3FROM ((Orders o INNER JOIN Customers c ON o.CustomerID =
4c.CustomerID)
5INNER JOIN Employee e ON o.EmployeeID = e.EmployeeID);
Best practices
1SELECT column_name
2FROM table_name AS alias_name;
1SELECT vendor_name
2,product_name
3,product_price
4FROM Vendors AS v, Products AS p
5WHERE v.vendor_id = p.vendor_id;
Self joins
EXAMPLES
Matching the customers from the same city:
Left Join
Return all records from the left table, and the matched records from the right table.
Right Join
Return all records from the right table, and the matched records from the left table.
Left Joins can be turned into right joins by reversing the order of tables.
Unions
The UNION operator is used to combine the result-set of two or more SELECT
statements.
Each SELECT statement with UNION must have the same number of columns.
Note
Summary
Best practices using joins
Check the number of records.
Check for duplicates.
Check the number of records each time you make a new join.
Start smart: one table at a time.
Join syntax
Further readings
1. Using a subquery, find the names of all the tracks for the album
“Californication”.
1SELECT Name
2FROM Tracks
3WHERE AlbumId IN (
4SELECT AlbumId
5FROM Albums
6WHERE Title = "Californication");
1+-------------------+
2| Name |
3+-------------------+
4| Around The World |
5| Parallel Universe |
6| Scar Tissue |
7| Otherside |
8| Get On Top |
9| Californication |
10| Easily |
11| Porcelain |
12| Emit Remmus |
13| I Like Dirt |
14+-------------------+
15(Output limit exceeded, 10 of 15 total rows shown)
2. Find the total number of invoices for each customer along with the customer’s
full name, city and email.
1SELECT Email,City,FirstName,
2(SELECT COUNT(*)
3FROM Invoices
4WHERE Invoices.CustomerId = Customers.CustomerId) AS Numbers
5FROM Customers;
+--------------------------+---------------------+-----------
+---------+
| Email | City | FirstName |
Numbers |
+--------------------------+---------------------+-----------
+---------+
| [email protected] | São José dos Campos | Luís |
17 |
2| [email protected] | Stuttgart | Leonie |
37 |
4| [email protected] | Montréal | François |
57 |
6| [email protected] | Oslo | Bjørn |
77 |
8| [email protected] | Prague | František |
97 |
10| [email protected] | Prague | Helena |
117 |
12| [email protected] | Vienne | Astrid |
137 |
14| [email protected] | Brussels | Daan |
157 |
| [email protected] | Copenhagen | Kara |
7 |
| [email protected] | São Paulo | Eduardo |
7 |
+--------------------------+---------------------+-----------
+---------+
(Output limit exceeded, 10 of 59 total rows shown)
3. Retrieve the track name, album, artist, and trackID for all the albums.
1SELECT Tracks.Name, Tracks.trackID, Albums.Title, Albums.ArtistId
2FROM Albums
3LEFT JOIN Tracks
4ON Albums.AlbumId = Tracks.AlbumId;
+-----------------------------------------+---------
+---------------------------------------+----------+
| Name | TrackId | Title
| ArtistId |
+-----------------------------------------+---------
+---------------------------------------+----------+
| For Those About To Rock (We Salute You) | 1 | For Those
1About To Rock We Salute You | 1 |
2| Put The Finger On You | 6 | For Those
3About To Rock We Salute You | 1 |
4| Let's Get It Up | 7 | For Those
5About To Rock We Salute You | 1 |
6| Inject The Venom | 8 | For Those
7About To Rock We Salute You | 1 |
8| Snowballed | 9 | For Those
9About To Rock We Salute You | 1 |
10| Evil Walks | 10 | For Those
11About To Rock We Salute You | 1 |
12| C.O.D. | 11 | For Those
13About To Rock We Salute You | 1 |
14| Breaking The Rules | 12 | For Those
15About To Rock We Salute You | 1 |
| Night Of The Long Knives | 13 | For Those
About To Rock We Salute You | 1 |
| Spellbound | 14 | For Those
About To Rock We Salute You | 1 |
+-----------------------------------------+---------
+---------------------------------------+----------+
(Output limit exceeded, 10 of 3503 total rows shown)
4. Retrieve a list with the managers last name, and the last name of the
employees who report to him or her.
5. Find the name and ID of the artists who do not have albums.
1SELECT Artists.Name, Artists.ArtistId
2FROM Artists
3LEFT JOIN Albums
4ON Artists.ArtistId = Albums.ArtistId
5WHERE Albums.AlbumId IS NULL;
1+----------------------------+----------+
2| Name | ArtistId |
3+----------------------------+----------+
4| Milton Nascimento & Bebeto | 25 |
5| Azymuth | 26 |
6| João Gilberto | 28 |
7| Bebel Gilberto | 29 |
8| Jorge Vercilo | 30 |
9| Baby Consuelo | 31 |
10| Ney Matogrosso | 32 |
11| Luiz Melodia | 33 |
12| Nando Reis | 34 |
13| Pedro Luís & A Parede | 35 |
14+----------------------------+----------+
15(Output limit exceeded, 10 of 71 total rows shown)
6. Use a UNION to create a list of all the employee’s & customer’s first names
and last names ordered by the last name in descending order.
1SELECT FirstName,LastName
2FROM Employees
3UNION
4SELECT FirstName,LastName
5FROM Customers
6ORDER BY LastName DESC;
1+-----------+--------------+
2| FirstName | LastName |
3+-----------+--------------+
4| Fynn | Zimmermann |
5| Stanisław | Wójcik |
6| František | Wichterlová |
7| Johannes | Van der Berg |
8| François | Tremblay |
9| Mark | Taylor |
10| Ellie | Sullivan |
11| Victor | Stevens |
12| Puja | Srivastava |
13| Jack | Smith |
14+-----------+--------------+
15(Output limit exceeded, 10 of 67 total rows shown)
7. See if there are any customers who have a different city listed in their billing
city versus their customer city.
1SELECT COUNT(*)
2FROM Customers
3LEFT JOIN Invoices
4ON Customers.CustomerId = Invoices.CustomerId
5WHERE Customers.City <> Invoices.BillingCity;
1+----------+
2| COUNT(*) |
3+----------+
4| 0 |
5+----------+
Support Joins
String Functions:
Concatenations
1SELECT
2CompanyName
3,ContactName
4,CompanyName || '(' || ContactName || ')'
5FROM customers;
SQL server supports + instead of ||
Trimming strings
Trims the leading or trailing space from a string
TRIM
RTRIM
LTRIM
Substring
Returns the specified number of characters from a particular position of a given strings.
1SUBSTR(string name, string position, number of characters to be
returned)
1SELECT firstname
2,SUBSTR(firstname,2,3)
3FROM employees;
Upper and Lower
1SELECT UPPER(column_name) FROM tablename;