SlideShare a Scribd company logo
1 of 18
1
Structured Query Language (SQL)
What is SQL?
 Is a language of database, it includes database creation,
deletion, fetching rows and modifying rows etc.
 SQL is Structured Query Language, which is a computer
language for storing, manipulating and retrieving data stored in
relational database.
2
Why SQL?
• Allows users to access data in relational database management
systems.
• Allows users to describe the data.
• Allows users to define the data in database and manipulate that data.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views
3
SQL Syntax
• SQL is followed by unique set of rules and guidelines called Syntax. This
tutorial gives you a quick start with SQL by listing all the basic SQL
Syntax:
• All the SQL statements start with any of the keywords like SELECT,
INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW and
all the statements end with a semicolon (;).
4
SQL Syntax
• Important point to be noted is that SQL is case insensitive, which
means SELECT and select have same meaning in SQL statements, but
MySQL makes difference in table names. So if you are working with
MySQL, then you need to give table names as they exist in the
database.
5
SQL Statements
Most of the actions you need to perform on a database are done with SQL
statements.
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL
statement. Semicolon is the standard way to separate each SQL statement
in database systems that allow more than one SQL statement to be
executed in the same call to the server.
6
SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation
Language (DML) and the Data Definition Language (DDL).
7
DDL - Data Definition Language:
The DDL part of SQL permits database tables to be created or
deleted. It also defines indexes (keys), specifies links between
tables, and imposes constraints between tables. The most
important DDL statements in SQL are:
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
8
DDL - Data Definition Language:
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index
9
DML - Data Manipulation Language:
The query and update commands form the DML part of SQL:
 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
10
1. SQL SELECT Statement
The SELECT statement is used to select data from a database. The result
is stored in a result table, called the result-set.
SQL SELECT Syntax:
SELECT column_name(s)
FROM table_name
And
SELECT * FROM table_name
Note: SQL is not case sensitive. SELECT is the same as select.
11
Example: SELECT
Table Name: Students
StudentsID LastName FirstName Address City
1 Cabuso Fernan San Pedro Puerto Princesa
2 Porras Ellen Grace Alfonso XIII Quezon
3 Dela Cruz Steven Tabon Quezon
Now we want to select the content of the columns named "LastName" and
"FirstName" from the table above.
We use the following SELECT statement:
SELECT LastName, FirstName
FROM Students
12
Example: SELECT
Table Name: Students
The result-set will look like this:
LastName FirstName
Cabuso Fernan
Porras Ellen Grace
Dela Cruz Steven
13
Example: SELECT
Table Name: Students
Now we want to select all the columns from the “Students"
table. We use the following SELECT statement:
SELECT *
FROM Students
Or
SELECT * FROM Students
Tip: The asterisk (*) is a quick way of selecting all columns!
14
Example: SELECT
Table Name: Students
The result-set will look like this:
StudentsID LastName FirstName Address City
1 Cabuso Fernan San Pedro Puerto Princesa
2 Porras Ellen Grace Alfonso XIII Quezon
3 Dela Cruz Steven Tabon Quezon
15
2. SQL SELECT DISTINCT Statement
In a table, some of the columns may contain duplicate values. This is
not a problem, however, sometimes you will want to list only the
different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different)
values.
SQL SELECT DISTINCT Syntax:
SELECT DISTINCT column_name(s)
FROM table_name
16
Example: SELECT DISTINCT
Table Name: Students
Now we want to select only the distinct values from the column
named "City" from the table above.
StudentsID LastName FirstName Address City
1 Cabuso Fernan San Pedro Puerto Princesa
2 Porras Ellen Grace Alfonso XIII Quezon
3 Dela Cruz Steven Tabon Quezon
17
Example: SELECT DISTINCT
Table Name: Students
We use the following SELECT statement:
SELECT DISTINCT City FROM Students
The result-set will look like this:
City
Puerto Princesa
Quezon
Thank you

More Related Content

Similar to Structured Query Language (SQL).pptx

SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsRSolutions
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .MayankSinghRawat6
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxEliasPetros
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfTamiratDejene1
 
SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2Raj vardhan
 
Sql – Structured Query Language
Sql – Structured Query LanguageSql – Structured Query Language
Sql – Structured Query Languagepandey3045_bit
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowPavithSingh
 
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptxFayChan8
 

Similar to Structured Query Language (SQL).pptx (20)

SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Unit - II.pptx
Unit - II.pptxUnit - II.pptx
Unit - II.pptx
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutions
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Advanced sql injection 1
Advanced sql injection 1Advanced sql injection 1
Advanced sql injection 1
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
SQL2.pptx
SQL2.pptxSQL2.pptx
SQL2.pptx
 
SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Sql – Structured Query Language
Sql – Structured Query LanguageSql – Structured Query Language
Sql – Structured Query Language
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
 
Reviewing SQL Concepts
Reviewing SQL ConceptsReviewing SQL Concepts
Reviewing SQL Concepts
 
MySQL intro
MySQL introMySQL intro
MySQL intro
 
MySQL intro
MySQL introMySQL intro
MySQL intro
 

More from EllenGracePorras

Lesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptxLesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptxEllenGracePorras
 
Lesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptxLesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptxEllenGracePorras
 
Geographic Information System(GIS).pptx
Geographic  Information System(GIS).pptxGeographic  Information System(GIS).pptx
Geographic Information System(GIS).pptxEllenGracePorras
 
Geographic Information Systems (GIS).pptx
Geographic  Information Systems (GIS).pptxGeographic  Information Systems (GIS).pptx
Geographic Information Systems (GIS).pptxEllenGracePorras
 
Advanced Database Systems.pptx
Advanced Database Systems.pptxAdvanced Database Systems.pptx
Advanced Database Systems.pptxEllenGracePorras
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 
Advanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptxAdvanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptxEllenGracePorras
 
Advanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptxAdvanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptxEllenGracePorras
 
Structured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxStructured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxEllenGracePorras
 
.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptx.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptxEllenGracePorras
 

More from EllenGracePorras (16)

Lesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptxLesson 3 Introduction to Human Computer Interaction.pptx
Lesson 3 Introduction to Human Computer Interaction.pptx
 
Lesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptxLesson 4 Introduction to Human Computer Interaction.pptx
Lesson 4 Introduction to Human Computer Interaction.pptx
 
Geographic Information System(GIS).pptx
Geographic  Information System(GIS).pptxGeographic  Information System(GIS).pptx
Geographic Information System(GIS).pptx
 
Geographic Information Systems (GIS).pptx
Geographic  Information Systems (GIS).pptxGeographic  Information Systems (GIS).pptx
Geographic Information Systems (GIS).pptx
 
Advanced Database Systems.pptx
Advanced Database Systems.pptxAdvanced Database Systems.pptx
Advanced Database Systems.pptx
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Advanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptxAdvanced Database Systems - Presentation 4.pptx
Advanced Database Systems - Presentation 4.pptx
 
Advanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptxAdvanced Database Systems - Presentation 1 with quiz.pptx
Advanced Database Systems - Presentation 1 with quiz.pptx
 
Structured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxStructured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptx
 
SQL Where Clause.pptx
SQL Where Clause.pptxSQL Where Clause.pptx
SQL Where Clause.pptx
 
SQL Statements.pptx
SQL Statements.pptxSQL Statements.pptx
SQL Statements.pptx
 
Information Management
Information ManagementInformation Management
Information Management
 
Lesson 2 HCI 2.pptx
Lesson 2 HCI 2.pptxLesson 2 HCI 2.pptx
Lesson 2 HCI 2.pptx
 
Lesson 2 HCI 2.pdf
Lesson 2 HCI 2.pdfLesson 2 HCI 2.pdf
Lesson 2 HCI 2.pdf
 
.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptx.Net Technologies Lesson 1.pptx
.Net Technologies Lesson 1.pptx
 
CC 1/L Introduction.pptx
CC 1/L Introduction.pptxCC 1/L Introduction.pptx
CC 1/L Introduction.pptx
 

Recently uploaded

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Structured Query Language (SQL).pptx

  • 1. 1 Structured Query Language (SQL) What is SQL?  Is a language of database, it includes database creation, deletion, fetching rows and modifying rows etc.  SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in relational database.
  • 2. 2 Why SQL? • Allows users to access data in relational database management systems. • Allows users to describe the data. • Allows users to define the data in database and manipulate that data. • Allows users to create and drop databases and tables. • Allows users to create view, stored procedure, functions in a database. • Allows users to set permissions on tables, procedures and views
  • 3. 3 SQL Syntax • SQL is followed by unique set of rules and guidelines called Syntax. This tutorial gives you a quick start with SQL by listing all the basic SQL Syntax: • All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW and all the statements end with a semicolon (;).
  • 4. 4 SQL Syntax • Important point to be noted is that SQL is case insensitive, which means SELECT and select have same meaning in SQL statements, but MySQL makes difference in table names. So if you are working with MySQL, then you need to give table names as they exist in the database.
  • 5. 5 SQL Statements Most of the actions you need to perform on a database are done with SQL statements. Semicolon after SQL Statements? Some database systems require a semicolon at the end of each SQL statement. Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
  • 6. 6 SQL DML and DDL SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).
  • 7. 7 DDL - Data Definition Language: The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are:  CREATE DATABASE - creates a new database  ALTER DATABASE - modifies a database  CREATE TABLE - creates a new table
  • 8. 8 DDL - Data Definition Language:  ALTER TABLE - modifies a table  DROP TABLE - deletes a table  CREATE INDEX - creates an index (search key)  DROP INDEX - deletes an index
  • 9. 9 DML - Data Manipulation Language: The query and update commands form the DML part of SQL:  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database
  • 10. 10 1. SQL SELECT Statement The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SQL SELECT Syntax: SELECT column_name(s) FROM table_name And SELECT * FROM table_name Note: SQL is not case sensitive. SELECT is the same as select.
  • 11. 11 Example: SELECT Table Name: Students StudentsID LastName FirstName Address City 1 Cabuso Fernan San Pedro Puerto Princesa 2 Porras Ellen Grace Alfonso XIII Quezon 3 Dela Cruz Steven Tabon Quezon Now we want to select the content of the columns named "LastName" and "FirstName" from the table above. We use the following SELECT statement: SELECT LastName, FirstName FROM Students
  • 12. 12 Example: SELECT Table Name: Students The result-set will look like this: LastName FirstName Cabuso Fernan Porras Ellen Grace Dela Cruz Steven
  • 13. 13 Example: SELECT Table Name: Students Now we want to select all the columns from the “Students" table. We use the following SELECT statement: SELECT * FROM Students Or SELECT * FROM Students Tip: The asterisk (*) is a quick way of selecting all columns!
  • 14. 14 Example: SELECT Table Name: Students The result-set will look like this: StudentsID LastName FirstName Address City 1 Cabuso Fernan San Pedro Puerto Princesa 2 Porras Ellen Grace Alfonso XIII Quezon 3 Dela Cruz Steven Tabon Quezon
  • 15. 15 2. SQL SELECT DISTINCT Statement In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. SQL SELECT DISTINCT Syntax: SELECT DISTINCT column_name(s) FROM table_name
  • 16. 16 Example: SELECT DISTINCT Table Name: Students Now we want to select only the distinct values from the column named "City" from the table above. StudentsID LastName FirstName Address City 1 Cabuso Fernan San Pedro Puerto Princesa 2 Porras Ellen Grace Alfonso XIII Quezon 3 Dela Cruz Steven Tabon Quezon
  • 17. 17 Example: SELECT DISTINCT Table Name: Students We use the following SELECT statement: SELECT DISTINCT City FROM Students The result-set will look like this: City Puerto Princesa Quezon