SQL Introduction
SQL Introduction
2
Table of Contents
4. Selecting Data From Multiple Tables
Cartesian Product
Inner Joins with ON Clause
Left, Right and Full Outer Joins
Cross Joins
5. Inserting Data – INSERT
6. Modifying Data – UPDATE
7. Deleting Data – DELETE
3
Relational Databases
and SQL
The SQL Execution Model
Relational Databases and SQL
Name
Engineering
t i s re tu rned
Sales The resul o rd set)
a re c
Marketing (usually as
…
6
SQL Execution
7
SQL and T-SQL
Introduction
What is SQL?
GRANT, REVOKE
9
SQL – Few Examples
SELECT FirstName, LastName, JobTitle FROM Employees
UPDATE Projects
SET EndDate = '8/31/2006'
WHERE StartDate = '1/1/2006'
11
T-SQL – Example
CREATE PROCEDURE EmpDump AS
DECLARE @EmpId INT, @EmpFName NVARCHAR(100), @EmpLName NVARCHAR(100)
DECLARE emps CURSOR FOR
SELECT EmployeeID, FirstName, LastName FROM Employees
OPEN emps
FETCH NEXT FROM emps INTO @EmpId, @EmpFName, @EmpLName
WHILE (@@FETCH_STATUS = 0) BEGIN
PRINT CAST(@EmpId AS VARCHAR(10)) + ' '
+ @EmpFName + ' ' + @EmpLName
FETCH NEXT FROM emps INTO @EmpId, @EmpFName, @EmpLName
END
CLOSE emps
DEALLOCATE emps
GO 12
SQL Language
Introducing the SELECT Statement
Capabilities of SQL SELECT
Projection Selection
Take a subset of the columns Take a subset of the rows
Join
Combine tables
by
some column Table 1 Table14 2
The SoftUni Database Schema in SQL
Server
15
Basic SELECT Statement
16
SELECT – Example
Selecting all columns from the "Departments" table
… …
Arithmetic Operations
LastName ManagerID
NULL
NULL is displayed as
Sánchez
empty string or as
Duffy 300
"NULL"
Wang 1
… … 19
Column Aliases
Aliases rename a column heading
Immediately follows the column name (an
optional AS keyword)
Use "Some Name" or [Some Name] when the alias
contains spaces
SELECT FirstName, LastName, Salary, Salary*0.2 AS Bonus,
Salary * 0.2 / 12 AS "Monthly Bonus" FROM Employees
Our Employees
Guy's last name is Gilbert
Kevin's last name is Brown
Roberto's last name is Tamburello
…
22
Removing Duplicate Rows
By default SELECT returns all rows, including duplicate rows
DepartmentID
7
SELECT DepartmentID 7
FROM Employees
2
Use DISTINCT keyword to eliminate duplicated rows: ...
DepartmentID
SELECT 7
DISTINCT DepartmentID
2
FROM Employees
... 23
Set Operations: UNION, INTERSECT and
EXCEPT
UNION combines the results from several SELECT statements
The columns count and types should match
Name
SELECT FirstName AS Name FROM Employees A. Scott
UNION Abbas
SELECT LastName AS Name FROM Employees Abercrombie
...
INTERSECT / EXCEPT perform logical intersection / difference between two
sets of records
24
Filtering the Selected Rows
LastName DepartmentI
Restrict the rows returned by using the WHERE clause:
D
Tamburell 1
SELECT LastName, DepartmentID o
FROM Employees Erickson 1
WHERE DepartmentID = 1
Goldberg 1
More examples:
... ...
LastName HireDate
Valdez 2005-07-01
SELECT LastName, HireDate FROM Tsoflias 2005-07-01
Employees ORDER BY HireDate DESC Abbas 2005-04-15
… 29
…
Select with Paging in SQL Server
TownID Name
1 Redmond
Select the first 5 rows only: 2 Calgary
3 Edmonds
SELECT TOP 5 * FROM Towns
4 Seattle
Select rows from 20 to 24: 5 Bellevue
TownID Name
SELECT * FROM Towns 1 Monroe
ORDER BY Name 2 Nevada
OFFSET 20 ROWS 3 Newport Hills
FETCH NEXT 5 ROWS ONLY 4 Ottawa
5 30Portland
SQL Language
Joins: Selecting Data From Multiple Tables
Data from Multiple Tables
DepartmentI Name
Sometimes you need data from several tables:
D
1 Engineerin
LastNam DepartmentI g
e D 2 Tool design
Duffy 1 3 Sales
Abbas 3
Galvin 2
LastName DepartmentNam
e
Duffy Engineering
Galvin Tool design
Abbas Sales 32
Cartesian Product
This will produce Cartesian product:
The result:
LastName DepartmentName
Duffy Document Control
Wang Document Control
Sullivan Document Control
Duffy Engineering
Wang Engineering
… … 33
Cartesian Product (2)
34
Types of Joins
Inner joins
Left, right and full outer joins
Cross joins
35
Inner Join with ON Clause
To specify a join conditions, use the INNER JOIN … ON clause
DepartmentI DepartmentI
EmployeeID LastName DepartmentName
D D
1 Gilbert 7 7 Production
2 Brown 4 4 Marketing
3 Tamburello 1 1 Engineering
36
… … … … …
Equijoins
Equijoin == join conditions pushed down to the WHERE clause
DepartmentI DepartmentI
EmployeeID LastName DepartmentName
D D
1 Gilbert 7 7 Production
2 Brown 4 4 Marketing
3 Tamburello 1 1 Engineering
37
… … … … …
INNER vs. OUTER Joins
Inner join
A join of two tables returning only rows matching the join condition
Left (or right) outer join
Returns the results of the inner join as well as unmatched rows from the left (or
right) table
Full outer join
Returns the results of an inner join along with al unmatched rows
38
INNER JOIN
SELECT e.LastName EmpLastName,
m.EmployeeID MgrID, m.LastName MgrLastName
FROM Employees e INNER JOIN Employees m
ON e.ManagerID = m.EmployeeID
Message
Ovidiu Cracium is managed by Tamburello
Michael Sullivan is managed by Tamburello
Sharon Salavaria is managed by Tamburello
Dylan Miller is managed by Tamburello
… 44
Cross Join
TheCROSS JOIN clause produces the cross-
product of two tables
Same as a Cartesian product, rarely used
SELECT LastName [Last Name], Name [Dept Name]
FROM Employees CROSS JOIN Departments
UPDATE Employees
SET LastName = 'Brown'
WHERE EmployeeID = 1
UPDATE Employees
SET Salary = Salary * 1.10, JobTitle = 'Senior ' + JobTitle
WHERE DepartmentID = 3 52
Updating Joined Tables
We can update tables based on condition from joined tables
UPDATE Employees
SET JobTitle = 'Senior ' + JobTitle
FROM Employees e
JOIN Departments d
ON e.DepartmentID = d.DepartmentID
WHERE d.Name = 'Sales'
53
SQL Language: DELETE
Deleting Data From Tables
Deleting Data
Deleting rows from a table
DELETE FROM <table> WHERE <condition>
56
SQL Syntax in MySQL
MySQL Extensions to the Standard SQL
SQL Syntax in MySQL
SELECT * FROM city LIMIT 100, 10
SHOW TABLES
SQL Syntax in MySQL (2)
Show table schema
DESCRIBE <table_name>
Replace data (delete + insert)
REPLACE INTO city(ID, Name, CountryCode, District, Population)
VALUES(100000, 'Kaspichan', 'BGR', 'Shoumen', 3300);
Regular expression matching
SELECT '0888123456' REGEXP '[0-9]+'
Enumerations as column types