SlideShare a Scribd company logo
1 of 27
How to: SQL
By: Sam Loch
Goal
 To retrieve data from and insert data into a relational database
Customer
CustomerID
FirstName
LastName
Email
Phone
Address
Order
OrderID
CustomerID
OrderDate
ShippingInstruction
s
Order-Product
OrderProductID
OrderID
ProductID
Quantity
Product
ProductID
ProductName
Price
InventoryLevel
How to Accomplish Our Goal
 Structured Query Language (SQL)
 Using SQL we can
 Retrieve data from a database
 Insert data into a database
 Update data in a database
 Delete data in a database
 Add and delete tables form a database
 Combine tables in a database
Creating a Database
 Define the database
 Create tables
 Define columns within these tables and choose their data types
 Each table needs a Primary Key
 Some tables will need Foreign Key’s to communicate with other tables in the database
Creating a Database (2)
 CREATE statements
 These can be used to create a table within a database
 Example:
CREATE TABLE db_name.table_name (
ColumnName1 datatype NOT NULL,
ColumnName2 datatype NULL,
PRIMARY KEY (KeyName));
Text Description
NULL/NOT
NULL
Defines whether the field can be
empty (NULL) or whether it cannot
(NOT NULL)
Primary Key’s cannot be NULL
datatype The data-type of the column.
Creating a Database (3)
 Another Example:
CREATE TABLE salesdb.Order (
OrderID INT NOT NULL,
CustomerID INT NOT NULL,
OrderDate DATE NULL,
ShippingInstructions VARCHAR(45) NULL,
PRIMARY KEY (OrderID));
Order
OrderID
CustomerID
OrderDate
ShippingInstruction
s
Data Types
 Each column can contain a different type of data
 The data type of each column must be specified when it is created
 A few data types:
Data type Description Examples
INT Integer 4, 62, -20
DECIMAL(p,s) Decimal. Example: decimal(6,3) is a
number that has 3 digits after the
decimal and 6 digits total (123.456)
6.5, 0.9987, 123.456
VARCHAR(x) String of any characters with a
maximum length of x
‘Howdy’,
‘She sells seashells by
the sea shore’
DATETIME,
DATE
Date and time, or just Date '2017-05-03 20:43:00',
'2017-05-03'
BOOLEAN Boolean value 0,1
Foreign Keys
 The Foreign Key in one table connects that table to a Primary key in another table.
CREATE TABLE salesdb.`Order-Product` (
OrderProductID INT NOT NULL,
OrderID INT NULL,
ProductID INT NULL,
Quantity INT NULL,
PRIMARY KEY (OrderProductID),
FOREIGN KEY (OrderID) REFERENCES salesdb.Order(OrderID)
FOREIGN KEY (ProductID) REFERENCES salesdb.Product(ProductID)
Product
ProductID
ProductName
Price
InventoryLevel
Order-Product
OrderProductID
OrderID
ProductID
Quantity
ProductID is a foreign key in the
Order-Product table, and the
primary key in the Product table.
Deleting Tables
 DROP TABLE db_name.table_name;
 EX: DROP TABLE salesdb.Product;
Altering the Data in a Table
Adding a column:
ALTER TABLE db_name.table_name
ADD COLUMN column_name datatype (NULL or NOT NULL);
Deleting a column:
ALTER TABLE db_name.table_name
DROP COLUMN column_name;
Changing a column:
ALTER TABLE db_name.table_name
CHANGE COLUMN old_column_name
new_column_name datatype (NULL or NOT NULL);
Altering the Data in a Table (2)
 Examples:
ALTER TABLE salesdb.Product
ADD COLUMN Color VARCHAR(10) NULL;
ALTER TABLE salesdb.Product
DROP COLUMN Color;
ALTER TABLE salesdb.Product
CHANGE COLUMN Color
Version VARCHAR(20) NULL;
Adds “Color” column
to Product table
Changes “Color”
column from Product
table to “Version” and
changes character
limit from 10 to 20
Deletes “Color” column
to Product table
Adding a Row to a Table
INSERT INTO db_name.table_name
(ColumnName1, ColumnName2, ColumnName3)
VALUES (Value1, Value2, Value3);
 Example:
INSERT INTO salesdb.Customer
(CustomerID, FirstName, LastName, Email, Phone, Address)
VALUES (2003, ‘Kit’, ‘Fisto’, ‘KFisto@Gmail .com’, 717.555.1234, ‘560 Lois Lane
Pittsburgh, PA’)
CustomerID FirstName LastName Email Phone Address
1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA
2000 Dwight Shrute BeetBoi@Hotmail.co
m
717.555.5555 12 Farm Street Scranton, PA
2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA
2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA
2003 Kit Fisto Kfisto@gmail.com 717.555.123
4
560 Lois Lane Pittsburgh, PA
Changing a Row
UPDATE db_name.table_name
SET ColumnName1 = Value1, ColumnName2 = Value2
WHERE condition;
 Example:
UPDATE salesdb.Product
SET ProductName = ‘Shake Weight’, Price = 99.99
WHERE ProductID = 1341;
ProductID ProductName Price
1341 Shake Weight 99.99
1342 The Claw 29.99
1343 SHAM WOW 4.99
The Price of the Shake
Weight was changed to
99.99
Deleting a Row
DELETE FROM db_name.table_name
WHERE condition;
 Example:
DELETE FROM salesdb.Product
WHERE ProductID = 1738;
Retrieving Data From a Database
 SELECT statements
 SELECT statements are used to retrieve data from a database
 EX:
SELECT column_name(s)
FROM db_name.table_name;
SELECT statements
 If you had a customer table and wanted to see the first and last names of all
of the customers you would type:
SELECT FirstName, LastName
FROM salesdb.Customer
 This would return:
CustomerID FirstName LastName Email Phone Address
1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA
2000 Dwight Shrute BeetBoi@Hotmail.co
m
717.555.5555 12 Farm Street Scranton, PA
2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA
2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA
2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA
FirstName LastName
Johnny Bravo
Dwight Shrute
Patches O‘Houlihan
Eric Foreman
Kit Fisto
Retrieving All Columns in a Table
SELECT * FROM salesdb.Customer
CustomerID FirstName LastName Email Phone Address
1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA
2000 Dwight Shrute BeetBoi@Hotmail.co
m
717.555.5555 12 Farm Street Scranton, PA
2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA
2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA
2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA
Retrieving Values
 To retrieve only unique values:
SELECT DISTINCT Price
FROM salesdb.Product;
 To retrieve certain records only
SELECT * FROM salesdb.Product WHERE Price = 99.99;
SELECT * FROM salesdb.Product WHERE Price > 5;
WHERE Clause
 The following is a list of operators that can be used in the WHERE Clause:
Operator Description
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
Sorting Results of a Select Statement
SELECT * FROM salesdb.Customer
WHERE CustomerID >= 2000
ORDER BY FirstName;
CustomerID FirstName LastName Email Phone Address
2000 Dwight Shrute BeetBoi@Hotmail.co
m
717.555.5555 12 Farm Street Scranton, PA
2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA
2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA
2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA
ORDER BY Ascending (ASC) and
Descending (DESC)
SELECT * FROM salesdb.Product
ORDER BY Price ASC;
SELECT * FROM salesdb.Product
ORDER BY Price DESC;
ProductID ProductName Price
1341 Shake Weight 99.99
1342 The Claw 29.99
1343 SHAM WOW 4.99
ProductID ProductName Price
1343 Shake Weight 4.99
1342 The Claw 29.99
1341 SHAM WOW 99.99
Other SQL Functions
 COUNT() – Returns the number of rows
 MAX() – Returns the largest value
 MIN() – Returns the smallest value
 SUM() – Returns the sum
 AVG() – Returns the average value
Fun With Functions
SELECT MAX(Price) FROM salesdb.Product;
SELECT COUNT(ProductID) FROM salesdb.Product;
SELECT MIN(Price) FROM salesdb.Product;
SELECT SUM(Price) FROM salesdb.Product;
SELECT AVG(Price) FROM salesdb.Product;
ProductID ProductName Price
1341 Shake Weight 99.99
1342 The Claw 29.99
1343 SHAM WOW 4.99
Price
99.99
3
Price
4.99
Price
134.97
Price
44.99
SELECTing From Multiple Tables
 To SELECT from multiple tables you must join the tables with WHERE
 Example:
SELECT * FROM salesdb.Customer, salesdb.Order
WHERE Customer.CustomerID = Order.CustomerID;
 This Returns:
Custom
er.Cust
omerID
First
Nam
e
Last
Nam
e
Email Phone Address OrderID OrderDate Order.CustomerID
2000 Dwig
ht
Shru
te
BeetBoi@H
otmail.com
717.555.5
555
12 Farm Street Scranton, PA 102 2017-4-15 1002
2002 Eric Fore
man
Eforeman@
yahoo.com
215.555.9
876
1919 S. 19th Street
Philadelphia, PA
103 2017-4-25 1003
2003 Kit Fisto Kfisto@gm
ail.com
717.555.1
234
560 Lois Lane Pittsburgh, PA 104 2017-4-30 1004
2001 Patc
hes
O‘Ho
uliha
n
Dballislife
@gmail.co
m
717.555.0
000
34 Legend Lane Lancaster, PA 105 2017-5-3 1005
JOIN Syntax
SELECT * FROM salesdb.Customer, salesdb.Order
WHERE Customer.CustomerID = Order.CustomerID;
SELECT * Return all the columns from both
tables
FROM salesdb.Customer,
salesdb.Order
The two tables to be joined
WHERE Customer.CustomerID
= Order.CustomerID
Only choose records where the
CustomerID exists in both tables
Summary
 Use CREATE statements to create a table
 Use DROP TABLE to delete a table
 Use ALTER TABLE to change the data in a table
 ADD COLUMN
 DROP COLUMN
 CHANGE COLUMN
 Use INSERT INTO to add a row
 Use UPDATE and SET to change
a row
 Use DELETE FROM to delete a
row
Data type Description Examples
INT Integer 4, 62, -20
DECIMAL(p,s) Decimal. Example: decimal(6,3) is a
number that has 3 digits after the
decimal and 6 digits total (123.456)
6.5, 0.9987, 123.456
VARCHAR(x) String of any characters with a
maximum length of x
‘Howdy’,
‘She sells seashells by
the sea shore’
DATETIME,
DATE
Date and time, or just Date '2017-05-03 20:43:00',
'2017-05-03'
BOOLEAN Boolean value 0,1
Text Description
NULL/NOT
NULL
Defines whether the field can be
empty (NULL) or whether it cannot
(NOT NULL)
Primary Key’s cannot be NULL
datatype The data-type of the column.
Summary(2)
 Use SELECT statements to retrieve data from a database
 SELECT * selects all data from
 COUNT() – Returns the number of rows
 MAX() – Returns the largest value
 MIN() – Returns the smallest value
 SUM() – Returns the sum
 AVG() – Returns the average value

More Related Content

What's hot

Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLSql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLPrashant Kumar
 
[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05AnusAhmad
 
[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02AnusAhmad
 
Cobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkCobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkDataWorks Summit
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesAshwin Dinoriya
 
[Www.pkbulk.blogspot.com]dbms06
[Www.pkbulk.blogspot.com]dbms06[Www.pkbulk.blogspot.com]dbms06
[Www.pkbulk.blogspot.com]dbms06AnusAhmad
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersIván Stepaniuk
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3iccma
 
Intro To TSQL - Unit 2
Intro To TSQL - Unit 2Intro To TSQL - Unit 2
Intro To TSQL - Unit 2iccma
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatinMudasir Syed
 

What's hot (20)

Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLSql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
 
[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05[Www.pkbulk.blogspot.com]dbms05
[Www.pkbulk.blogspot.com]dbms05
 
Sql for biggner
Sql for biggnerSql for biggner
Sql for biggner
 
Sql osnova
Sql   osnovaSql   osnova
Sql osnova
 
[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02[Www.pkbulk.blogspot.com]dbms02
[Www.pkbulk.blogspot.com]dbms02
 
SImple SQL
SImple SQLSImple SQL
SImple SQL
 
MY SQL
MY SQLMY SQL
MY SQL
 
Sql
SqlSql
Sql
 
Sql intro
Sql introSql intro
Sql intro
 
Query
QueryQuery
Query
 
Cobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkCobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for Spark
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
[Www.pkbulk.blogspot.com]dbms06
[Www.pkbulk.blogspot.com]dbms06[Www.pkbulk.blogspot.com]dbms06
[Www.pkbulk.blogspot.com]dbms06
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholders
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3
 
SQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and ProfitSQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and Profit
 
Intro To TSQL - Unit 2
Intro To TSQL - Unit 2Intro To TSQL - Unit 2
Intro To TSQL - Unit 2
 
Basic SQL
Basic SQLBasic SQL
Basic SQL
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatin
 

Similar to SQL How-To (20)

SQL
SQLSQL
SQL
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
SQL(database)
SQL(database)SQL(database)
SQL(database)
 
SQL Portfolio
SQL PortfolioSQL Portfolio
SQL Portfolio
 
My Sql concepts
My Sql conceptsMy Sql concepts
My Sql concepts
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
SQL for practice, SQL for practice, SQL for practicepdf
SQL for practice, SQL for practice, SQL for practicepdfSQL for practice, SQL for practice, SQL for practicepdf
SQL for practice, SQL for practice, SQL for practicepdf
 
Sql
SqlSql
Sql
 
Sql intro
Sql introSql intro
Sql intro
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Farheen abdul hameed ip project (MY SQL);
Farheen abdul hameed ip project (MY SQL);Farheen abdul hameed ip project (MY SQL);
Farheen abdul hameed ip project (MY SQL);
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL
SQLSQL
SQL
 
Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
PDBC
PDBCPDBC
PDBC
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
Chap 7
Chap 7Chap 7
Chap 7
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
 

Recently uploaded

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Recently uploaded (20)

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

SQL How-To

  • 1. How to: SQL By: Sam Loch
  • 2. Goal  To retrieve data from and insert data into a relational database Customer CustomerID FirstName LastName Email Phone Address Order OrderID CustomerID OrderDate ShippingInstruction s Order-Product OrderProductID OrderID ProductID Quantity Product ProductID ProductName Price InventoryLevel
  • 3. How to Accomplish Our Goal  Structured Query Language (SQL)  Using SQL we can  Retrieve data from a database  Insert data into a database  Update data in a database  Delete data in a database  Add and delete tables form a database  Combine tables in a database
  • 4. Creating a Database  Define the database  Create tables  Define columns within these tables and choose their data types  Each table needs a Primary Key  Some tables will need Foreign Key’s to communicate with other tables in the database
  • 5. Creating a Database (2)  CREATE statements  These can be used to create a table within a database  Example: CREATE TABLE db_name.table_name ( ColumnName1 datatype NOT NULL, ColumnName2 datatype NULL, PRIMARY KEY (KeyName)); Text Description NULL/NOT NULL Defines whether the field can be empty (NULL) or whether it cannot (NOT NULL) Primary Key’s cannot be NULL datatype The data-type of the column.
  • 6. Creating a Database (3)  Another Example: CREATE TABLE salesdb.Order ( OrderID INT NOT NULL, CustomerID INT NOT NULL, OrderDate DATE NULL, ShippingInstructions VARCHAR(45) NULL, PRIMARY KEY (OrderID)); Order OrderID CustomerID OrderDate ShippingInstruction s
  • 7. Data Types  Each column can contain a different type of data  The data type of each column must be specified when it is created  A few data types: Data type Description Examples INT Integer 4, 62, -20 DECIMAL(p,s) Decimal. Example: decimal(6,3) is a number that has 3 digits after the decimal and 6 digits total (123.456) 6.5, 0.9987, 123.456 VARCHAR(x) String of any characters with a maximum length of x ‘Howdy’, ‘She sells seashells by the sea shore’ DATETIME, DATE Date and time, or just Date '2017-05-03 20:43:00', '2017-05-03' BOOLEAN Boolean value 0,1
  • 8. Foreign Keys  The Foreign Key in one table connects that table to a Primary key in another table. CREATE TABLE salesdb.`Order-Product` ( OrderProductID INT NOT NULL, OrderID INT NULL, ProductID INT NULL, Quantity INT NULL, PRIMARY KEY (OrderProductID), FOREIGN KEY (OrderID) REFERENCES salesdb.Order(OrderID) FOREIGN KEY (ProductID) REFERENCES salesdb.Product(ProductID) Product ProductID ProductName Price InventoryLevel Order-Product OrderProductID OrderID ProductID Quantity ProductID is a foreign key in the Order-Product table, and the primary key in the Product table.
  • 9. Deleting Tables  DROP TABLE db_name.table_name;  EX: DROP TABLE salesdb.Product;
  • 10. Altering the Data in a Table Adding a column: ALTER TABLE db_name.table_name ADD COLUMN column_name datatype (NULL or NOT NULL); Deleting a column: ALTER TABLE db_name.table_name DROP COLUMN column_name; Changing a column: ALTER TABLE db_name.table_name CHANGE COLUMN old_column_name new_column_name datatype (NULL or NOT NULL);
  • 11. Altering the Data in a Table (2)  Examples: ALTER TABLE salesdb.Product ADD COLUMN Color VARCHAR(10) NULL; ALTER TABLE salesdb.Product DROP COLUMN Color; ALTER TABLE salesdb.Product CHANGE COLUMN Color Version VARCHAR(20) NULL; Adds “Color” column to Product table Changes “Color” column from Product table to “Version” and changes character limit from 10 to 20 Deletes “Color” column to Product table
  • 12. Adding a Row to a Table INSERT INTO db_name.table_name (ColumnName1, ColumnName2, ColumnName3) VALUES (Value1, Value2, Value3);  Example: INSERT INTO salesdb.Customer (CustomerID, FirstName, LastName, Email, Phone, Address) VALUES (2003, ‘Kit’, ‘Fisto’, ‘KFisto@Gmail .com’, 717.555.1234, ‘560 Lois Lane Pittsburgh, PA’) CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.co m 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.123 4 560 Lois Lane Pittsburgh, PA
  • 13. Changing a Row UPDATE db_name.table_name SET ColumnName1 = Value1, ColumnName2 = Value2 WHERE condition;  Example: UPDATE salesdb.Product SET ProductName = ‘Shake Weight’, Price = 99.99 WHERE ProductID = 1341; ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99 The Price of the Shake Weight was changed to 99.99
  • 14. Deleting a Row DELETE FROM db_name.table_name WHERE condition;  Example: DELETE FROM salesdb.Product WHERE ProductID = 1738;
  • 15. Retrieving Data From a Database  SELECT statements  SELECT statements are used to retrieve data from a database  EX: SELECT column_name(s) FROM db_name.table_name;
  • 16. SELECT statements  If you had a customer table and wanted to see the first and last names of all of the customers you would type: SELECT FirstName, LastName FROM salesdb.Customer  This would return: CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.co m 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA FirstName LastName Johnny Bravo Dwight Shrute Patches O‘Houlihan Eric Foreman Kit Fisto
  • 17. Retrieving All Columns in a Table SELECT * FROM salesdb.Customer CustomerID FirstName LastName Email Phone Address 1999 Johnny Bravo Jbravo@gmail.com 215.555.5678 1624 N. 18th Street Philadelphia, PA 2000 Dwight Shrute BeetBoi@Hotmail.co m 717.555.5555 12 Farm Street Scranton, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA
  • 18. Retrieving Values  To retrieve only unique values: SELECT DISTINCT Price FROM salesdb.Product;  To retrieve certain records only SELECT * FROM salesdb.Product WHERE Price = 99.99; SELECT * FROM salesdb.Product WHERE Price > 5;
  • 19. WHERE Clause  The following is a list of operators that can be used in the WHERE Clause: Operator Description = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to
  • 20. Sorting Results of a Select Statement SELECT * FROM salesdb.Customer WHERE CustomerID >= 2000 ORDER BY FirstName; CustomerID FirstName LastName Email Phone Address 2000 Dwight Shrute BeetBoi@Hotmail.co m 717.555.5555 12 Farm Street Scranton, PA 2002 Eric Foreman Eforeman@yahoo.com 215.555.9876 1919 S. 19th Street Philadelphia, PA 2003 Kit Fisto Kfisto@gmail.com 717.555.1234 560 Lois Lane Pittsburgh, PA 2001 Patches O‘Houlihan Dballislife@gmail.com 717.555.0000 34 Legend Lane Lancaster, PA
  • 21. ORDER BY Ascending (ASC) and Descending (DESC) SELECT * FROM salesdb.Product ORDER BY Price ASC; SELECT * FROM salesdb.Product ORDER BY Price DESC; ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99 ProductID ProductName Price 1343 Shake Weight 4.99 1342 The Claw 29.99 1341 SHAM WOW 99.99
  • 22. Other SQL Functions  COUNT() – Returns the number of rows  MAX() – Returns the largest value  MIN() – Returns the smallest value  SUM() – Returns the sum  AVG() – Returns the average value
  • 23. Fun With Functions SELECT MAX(Price) FROM salesdb.Product; SELECT COUNT(ProductID) FROM salesdb.Product; SELECT MIN(Price) FROM salesdb.Product; SELECT SUM(Price) FROM salesdb.Product; SELECT AVG(Price) FROM salesdb.Product; ProductID ProductName Price 1341 Shake Weight 99.99 1342 The Claw 29.99 1343 SHAM WOW 4.99 Price 99.99 3 Price 4.99 Price 134.97 Price 44.99
  • 24. SELECTing From Multiple Tables  To SELECT from multiple tables you must join the tables with WHERE  Example: SELECT * FROM salesdb.Customer, salesdb.Order WHERE Customer.CustomerID = Order.CustomerID;  This Returns: Custom er.Cust omerID First Nam e Last Nam e Email Phone Address OrderID OrderDate Order.CustomerID 2000 Dwig ht Shru te BeetBoi@H otmail.com 717.555.5 555 12 Farm Street Scranton, PA 102 2017-4-15 1002 2002 Eric Fore man Eforeman@ yahoo.com 215.555.9 876 1919 S. 19th Street Philadelphia, PA 103 2017-4-25 1003 2003 Kit Fisto Kfisto@gm ail.com 717.555.1 234 560 Lois Lane Pittsburgh, PA 104 2017-4-30 1004 2001 Patc hes O‘Ho uliha n Dballislife @gmail.co m 717.555.0 000 34 Legend Lane Lancaster, PA 105 2017-5-3 1005
  • 25. JOIN Syntax SELECT * FROM salesdb.Customer, salesdb.Order WHERE Customer.CustomerID = Order.CustomerID; SELECT * Return all the columns from both tables FROM salesdb.Customer, salesdb.Order The two tables to be joined WHERE Customer.CustomerID = Order.CustomerID Only choose records where the CustomerID exists in both tables
  • 26. Summary  Use CREATE statements to create a table  Use DROP TABLE to delete a table  Use ALTER TABLE to change the data in a table  ADD COLUMN  DROP COLUMN  CHANGE COLUMN  Use INSERT INTO to add a row  Use UPDATE and SET to change a row  Use DELETE FROM to delete a row Data type Description Examples INT Integer 4, 62, -20 DECIMAL(p,s) Decimal. Example: decimal(6,3) is a number that has 3 digits after the decimal and 6 digits total (123.456) 6.5, 0.9987, 123.456 VARCHAR(x) String of any characters with a maximum length of x ‘Howdy’, ‘She sells seashells by the sea shore’ DATETIME, DATE Date and time, or just Date '2017-05-03 20:43:00', '2017-05-03' BOOLEAN Boolean value 0,1 Text Description NULL/NOT NULL Defines whether the field can be empty (NULL) or whether it cannot (NOT NULL) Primary Key’s cannot be NULL datatype The data-type of the column.
  • 27. Summary(2)  Use SELECT statements to retrieve data from a database  SELECT * selects all data from  COUNT() – Returns the number of rows  MAX() – Returns the largest value  MIN() – Returns the smallest value  SUM() – Returns the sum  AVG() – Returns the average value