SQL Lesson 1
SQL Lesson 1
02 04
Relational Database Data normalization
MODULE 1.
GET STARTED WITH
TRANSACT - SQL
Module 1: Get started with transact - sql
Introduction to SQL
WHAT IS SQL?
• Query data.
WHAT IS DATA?
• Needs to be secure.
Module 1: Get started with transact - sql
WHAT IS DATABASE?
• A repository of data.
• Provides the functionality for adding, modifying
and querying that data.
• Different kinds of databases store data in
different forms.
Module 1: Get started with transact - sql
Relational Database
Relational Database
Module 1: Get started with transact - sql
Data Manipulation Language (DML) Data Definition Language (DDL) Data Control Language (DCL)
Statements for querying and Statements for defining database Statements for assigning
modifying data: objects: security permissions:
• SELECT • CREATE • GRANT
• INSERT • ALTER • REVOKE
• UPDATE • DROP • DENY
• DELETE
Module 1: Get started with transact - sql
Data Types
Exact Numeric Approximate Numeric Charater Date/Time Binary
Bigint Nchar
Nvarchar
Ntext
Module 1: Get started with transact - sql
CREATE
CREATE DATABASE Statement: create a new SQL database.
CREATE - Example
CREATE DATABASE Statement
ID Name phoneNumber Address
…
Module 1: Get started with transact - sql
SELECT
SELECT statement: select data from a database.
Specific columns
All columns
SELECT *
FROM table_name;
Module 1: Get started with transact - sql
WHERE
WHERE statement: used to filter records (extract only those records that fulfill a specified condition).
WHERE - Example
Selects all the customers
from the country "Mexico",
in the "Customers" table:
SELECT *
FROM Customers
WHERE Country = ‘Mexico’;
Module 1: Get started with transact - sql
WHERE
+ Addition
- Subtraction
* Multiplication
Arithmetic Operators
/ Division
= Equal
AND Return records that meet all the conditions separated by AND in WHERE clause.
Logical operators OR Return records that meet any of the conditions separated by OR in WHERE clause.
NOT Return records that do not satisfy any of the conditions in WHERE clause.
[NOT] IN Specify multiple values in a WHERE clause (a shorthand for multiple OR conditions)
SQL operators
IS [NOT] NULL Returns records having [NOT] NULL values in the given fields.
[NOT] LIKE Returns records that [DO NOT] match a specified pattern in a column.
Module 1: Get started with transact - sql
WHERE
IN BETWEEN
WHERE
LIKE
DISTINCT
DISTINCT - Example
Selects only the DISTINCT
values from the "Country"
column in the "Customers"
table:
TOP
TOP - Example
Selects the first three records
from the "Customers" table
SELECT TOP 3 *
FROM Customers
Module 1: Get started with transact - sql
COUNT
COUNT statement: returns the number of rows that matches a specified criterion.
SELECT COUNT(Column_name)
FROM table_name
WHERE condition;
SELECT COUNT(*)
FROM table_name;
Module 1: Get started with transact - sql
UPDATE
UPDATE statement: returns the number of rows that matches a specified criterion.
UPDATE table_name
SET column1= value1, column2 = value2,…
WHERE condition;
Module 1: Get started with transact - sql
UPDATE - Example
Updates the first customer
(CustomerID = 1) with a new
contact person and a new city.
UPDATE Customers
SET ContactName = 'Alfred
Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
Module 1: Get started with transact - sql
DELETE
DELETE statement: delete existing records in a table..
DELETE - Example
Deletes the customer "Alfreds
Futterkiste" from the
"Customers" table.
Introduction to SQL
• SQL, Data, Database
• DBMS, RDBMS
PRACTICE
Table: https://data.sfgov.org/Culture-and-Recreation/Film-Locations-in-San-Francisco/yitu-d5am
Module 1: Get started with transact - sql
PRACTICE
1. Retrieve the fun facts and filming locations of all films.
2. Retrieve the names of all films released in the 20th century and before (release years before 2000
including 2000) that, along with filming locations and release years.
3. Retrieve the names, production company names, filming locations, and release years of the films which
are not written by James Cameron.
4. Retrieve the number of locations of the films which are directed by Woody Allen.
5. Retrieve the number of films shot at Russian Hill.
6. Retrieve the number of rows having a release year older than 1950 from the "FilmLocations" table.
Module 1: Get started with transact - sql
PRACTICE
Table: Instructor
1. Insert a new instructor record with id 4 for Sandip Saha who lives in Edmonton, CA into the "Instructor" table.
2. Insert two new instructor records into the "Instructor" table. First record with id 5 for John Doe who lives in
Sydney, AU. Second record with id 6 for Jane Doe who lives in Dhaka, BD.
3. Update the city for Sandip to Toronto.
4. Update the city and country for Doe with id 5 to Dubai and AE respectively.
5. Remove the instructor record of Doe whose id is 6.
THANKS
CREDITS: This presentation template was created by
Slidesgo, including icons by Flaticon, and Name: Nguyễn Lê Chí Bảo
infographics & images by Freepik and illustrations Email: [email protected]
by Storyset Phone: 091 544 2420
Linkedin: nguyenlechibao