SlideShare a Scribd company logo
1 of 23
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
SELECT DISTINCT Person.PName
FROM Person;
Id Name Address Hobby
1123 Anita Damauli stamps
1123 Anita Damauli coins
5556 Binod
Kathmand
u
hiking
9876 Barsha
Kathmand
u
stamps
PName
Anita
Barsha
Binod
SQL AND & OR Operators
 The AND & OR operators are used to filter
records based on more than one condition
 The AND operator displays a record if both the
first condition and the second condition are
true.
 The OR operator displays a record if either the
first condition or the second condition is true.
SQL OR Operators
SELECT * FROM Person
WHERE Person.Hobby='hiking' OR
Person.ID>3000;
ID PName Address Hobby
5556 Binod Kathmandu hiking
9876 Barsha Kathmandu stamps
SQL AND Operators
SELECT *
FROM Person
WHERE Person.ID>3000 AND Person.ID<5999;
ID PName Address Hobby
5556 Binod Kathmandu hiking
SQL ORDER BY
 The ORDER BY keyword is used to sort the
result-set.
 The ORDER BY keyword is used to sort the
result-set by a specified column.
 The ORDER BY keyword sorts the records in
ascending order by default.
 If you want to sort the records in a descending
order, you can use the DESC keyword.
SQL ORDER BY Syntax
 SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
SELECT Person.Hobby
FROM Person
ORDER BY Person.Hobby;
Hobby
coins
hiking
stamps
stamps
SQL INSERT INTO
 The INSERT INTO statement is used to insert a new
row in a table.
SQL INSERT INTO Syntax
 It is possible to write the INSERT INTO statement in
two forms.
 The first form doesn't specify the column names
where the data will be inserted, only their values:
INSERT INTO table_name VALUES (value1, value2,
value3,...)
 The second form specifies both the column names
and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
SQL INSERT INTO
 INSERT INTO Persons
VALUES (4,'Nilsen', 'Johan', 'Bakken 2',
'Stavanger')
 INSERT INTO Persons (P_Id, LastName,
FirstName)
VALUES (5, 'Tjessem', 'Jakob')
SQL UPDATE
The UPDATE statement is used to update
existing records in a table.
SQL UPDATE Syntax:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example:
UPDATE Persons
SET Address=‘Nayabazar', City=‘Kathmandu'
WHERE LastName=‘Ghimire' AND
FirstName=‘Bishal'
SQL UPDATE Warning
 Be careful when updating records. If we had
omitted the WHERE clause in the example
above, like this:
UPDATE Persons
SET Address=‘Nayabazar', City=‘Kathmandu‘
What will be the result ?
SQL DELETE
 The DELETE statement is used to delete rows
in a table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value
DELETE FROM Persons
WHERE Address=‘Nayabazar', City=‘Kathmandu‘
SQL TOP
 The TOP clause is used to specify the number
of records to return.
 The TOP clause can be very useful on large
tables with thousands of records. Returning a
large number of records can impact on
performance.
 Note: Not all database systems support the
TOP clause.
 SQL Server Syntax
 SELECT TOP number|percent column_name(s)
FROM table_name
SQL TOP
 MySQL Syntax
 SELECT column_name(s)
FROM table_name
LIMIT number
 Oracle Syntax
 SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number
 SELECT * FROM Persons LIMIT 5
SQL LIKE
 The LIKE operator is used to search for a
specified pattern in a column.
 SQL LIKE Syntax
 SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
SQL LIKE
 To select the persons living in a city that starts
with "s" from the table.
 SELECT * FROM Persons
WHERE City LIKE ‘k%'
SQL Wildcards
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist]or
[!charlist]
Any single character not in charlist
• SQL wildcards can substitute for one or more
characters when searching for data in a
database.
• SQL wildcards must be used with the SQL
LIKE operator.
SQL IN
 The IN operator allows you to specify multiple
values in a WHERE clause.
 SQL IN Syntax
 SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
 Example #1
Select *
From Address
Where FirstName IN ('Mary', 'Sam')
 Example #2
SELECT *
FROM Address
WHERE FirstName = 'Mary'
OR FirstName = 'Sam'
SQL BETWEEN
 The BETWEEN operator selects a range of
data between two values. The values can be
numbers, text, or dates.
 SQL BETWEEN Syntax
 SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
 Example:
 SELECT * FROM suppliers WHERE supplier_id
BETWEEN 5000 AND 5010;
 SELECT * FROM suppliers WHERE supplier_id
>= 5000 AND supplier_id <= 5010;
 example:
 SELECT * FROM orders WHERE order_date
between to_date ('2013/01/01', 'yyyy/mm/dd')
AND to_date ('2013/12/31', 'yyyy/mm/dd');
 SELECT * FROM orders WHERE order_date >=
to_date('2013/01/01', 'yyyy/mm/dd') AND
order_date <=
to_date('2013/12/31','yyyy/mm/dd');

More Related Content

What's hot

What's hot (20)

Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
The Relational Database Model
The Relational Database ModelThe Relational Database Model
The Relational Database Model
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL
SQLSQL
SQL
 
Dbms keys
Dbms keysDbms keys
Dbms keys
 
Trigger
TriggerTrigger
Trigger
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
 

Viewers also liked

Prestations et aides sociales, le dérapage incontrôlé
Prestations et aides sociales, le dérapage incontrôléPrestations et aides sociales, le dérapage incontrôlé
Prestations et aides sociales, le dérapage incontrôlé
Fondation iFRAP
 
嶺南 完成版 With Reference By Vince Cheung
嶺南 完成版 With Reference  By Vince Cheung嶺南 完成版 With Reference  By Vince Cheung
嶺南 完成版 With Reference By Vince Cheung
alexwong1215
 
Setting Up Stock Broking Business in Vietnam
Setting Up Stock Broking Business in VietnamSetting Up Stock Broking Business in Vietnam
Setting Up Stock Broking Business in Vietnam
Dennis Ooi
 
[Paper introduction] Performance Capture of Interacting Characters with Handh...
[Paper introduction] Performance Capture of Interacting Characters with Handh...[Paper introduction] Performance Capture of Interacting Characters with Handh...
[Paper introduction] Performance Capture of Interacting Characters with Handh...
Mitsuru Nakazawa
 
Tmw20116 brooks.l
Tmw20116 brooks.lTmw20116 brooks.l
Tmw20116 brooks.l
navaidkhan
 

Viewers also liked (20)

Marketing pitch
Marketing pitchMarketing pitch
Marketing pitch
 
Prestations et aides sociales, le dérapage incontrôlé
Prestations et aides sociales, le dérapage incontrôléPrestations et aides sociales, le dérapage incontrôlé
Prestations et aides sociales, le dérapage incontrôlé
 
CIRCULAR IPC FEBRERO 2016
CIRCULAR IPC FEBRERO 2016CIRCULAR IPC FEBRERO 2016
CIRCULAR IPC FEBRERO 2016
 
嶺南 完成版 With Reference By Vince Cheung
嶺南 完成版 With Reference  By Vince Cheung嶺南 完成版 With Reference  By Vince Cheung
嶺南 完成版 With Reference By Vince Cheung
 
معلم التجويد
معلم التجويدمعلم التجويد
معلم التجويد
 
Setting Up Stock Broking Business in Vietnam
Setting Up Stock Broking Business in VietnamSetting Up Stock Broking Business in Vietnam
Setting Up Stock Broking Business in Vietnam
 
[Paper introduction] Performance Capture of Interacting Characters with Handh...
[Paper introduction] Performance Capture of Interacting Characters with Handh...[Paper introduction] Performance Capture of Interacting Characters with Handh...
[Paper introduction] Performance Capture of Interacting Characters with Handh...
 
Walden UDL PPT
Walden UDL PPTWalden UDL PPT
Walden UDL PPT
 
gghjkl
gghjklgghjkl
gghjkl
 
Funding outlook for training providers by Safaraz Ali April 2015
Funding outlook for training providers by Safaraz Ali April 2015Funding outlook for training providers by Safaraz Ali April 2015
Funding outlook for training providers by Safaraz Ali April 2015
 
15 May 2015
15 May 201515 May 2015
15 May 2015
 
Ee 2484i
Ee 2484iEe 2484i
Ee 2484i
 
Knives, Sharpeners, Scissors, Graters and Hooks.
Knives, Sharpeners, Scissors, Graters and Hooks.Knives, Sharpeners, Scissors, Graters and Hooks.
Knives, Sharpeners, Scissors, Graters and Hooks.
 
History and actors of nonviolence. — 06. From 1939 to 1949
History and actors of nonviolence. — 06. From 1939 to 1949History and actors of nonviolence. — 06. From 1939 to 1949
History and actors of nonviolence. — 06. From 1939 to 1949
 
TestBird - Mobile Game Testing Report(Sample)
TestBird - Mobile Game Testing Report(Sample)TestBird - Mobile Game Testing Report(Sample)
TestBird - Mobile Game Testing Report(Sample)
 
Apple 301
Apple 301Apple 301
Apple 301
 
Sfdfdf
SfdfdfSfdfdf
Sfdfdf
 
Tmw20116 brooks.l
Tmw20116 brooks.lTmw20116 brooks.l
Tmw20116 brooks.l
 
hlooooooo
hlooooooohlooooooo
hlooooooo
 
Resume: Research Engineer
Resume: Research Engineer Resume: Research Engineer
Resume: Research Engineer
 

Similar to 06.01 sql select distinct

Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
sagaroceanic11
 
Sql – Structured Query Language
Sql – Structured Query LanguageSql – Structured Query Language
Sql – Structured Query Language
pandey3045_bit
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
Iblesoft
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
ISsoft
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsgADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 

Similar to 06.01 sql select distinct (20)

MY SQL
MY SQLMY SQL
MY SQL
 
SQL
SQLSQL
SQL
 
Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01Mysql 120831075600-phpapp01
Mysql 120831075600-phpapp01
 
Sql – Structured Query Language
Sql – Structured Query LanguageSql – Structured Query Language
Sql – Structured Query Language
 
SQL notes 1.pdf
SQL notes 1.pdfSQL notes 1.pdf
SQL notes 1.pdf
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
SQL
SQLSQL
SQL
 
SQL
SQLSQL
SQL
 
SQL Language
SQL LanguageSQL Language
SQL Language
 
Query
QueryQuery
Query
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 
Sql slid
Sql slidSql slid
Sql slid
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
Sql
SqlSql
Sql
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
 
SQL report
SQL reportSQL report
SQL report
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsgADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdfClass XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
 

More from Bishal Ghimire

09.02 normalization example
09.02 normalization example09.02 normalization example
09.02 normalization example
Bishal Ghimire
 
07.03 cartesian product
07.03 cartesian product07.03 cartesian product
07.03 cartesian product
Bishal Ghimire
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
Bishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
Bishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
Bishal Ghimire
 
02.02 querying relational database
02.02 querying relational database02.02 querying relational database
02.02 querying relational database
Bishal Ghimire
 
02.01 relational databases
02.01 relational databases02.01 relational databases
02.01 relational databases
Bishal Ghimire
 
00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus
Bishal Ghimire
 

More from Bishal Ghimire (17)

Counseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental HealthCounseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental Health
 
Agile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over ProcessAgile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over Process
 
Earthquake in Nepal 2015
Earthquake in Nepal 2015Earthquake in Nepal 2015
Earthquake in Nepal 2015
 
09.02 normalization example
09.02 normalization example09.02 normalization example
09.02 normalization example
 
07.05 division
07.05 division07.05 division
07.05 division
 
07.04 joins
07.04 joins07.04 joins
07.04 joins
 
07.03 cartesian product
07.03 cartesian product07.03 cartesian product
07.03 cartesian product
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
 
09.01 normalization
09.01 normalization09.01 normalization
09.01 normalization
 
06.02 sql alias
06.02 sql alias06.02 sql alias
06.02 sql alias
 
04.01 file organization
04.01 file organization04.01 file organization
04.01 file organization
 
02.02 querying relational database
02.02 querying relational database02.02 querying relational database
02.02 querying relational database
 
02.01 relational databases
02.01 relational databases02.01 relational databases
02.01 relational databases
 
01.01 introduction to database
01.01 introduction to database01.01 introduction to database
01.01 introduction to database
 
00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

06.01 sql select distinct

  • 1.
  • 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
  • 3. SELECT DISTINCT Person.PName FROM Person; Id Name Address Hobby 1123 Anita Damauli stamps 1123 Anita Damauli coins 5556 Binod Kathmand u hiking 9876 Barsha Kathmand u stamps PName Anita Barsha Binod
  • 4. SQL AND & OR Operators  The AND & OR operators are used to filter records based on more than one condition  The AND operator displays a record if both the first condition and the second condition are true.  The OR operator displays a record if either the first condition or the second condition is true.
  • 5. SQL OR Operators SELECT * FROM Person WHERE Person.Hobby='hiking' OR Person.ID>3000; ID PName Address Hobby 5556 Binod Kathmandu hiking 9876 Barsha Kathmandu stamps
  • 6. SQL AND Operators SELECT * FROM Person WHERE Person.ID>3000 AND Person.ID<5999; ID PName Address Hobby 5556 Binod Kathmandu hiking
  • 7. SQL ORDER BY  The ORDER BY keyword is used to sort the result-set.  The ORDER BY keyword is used to sort the result-set by a specified column.  The ORDER BY keyword sorts the records in ascending order by default.  If you want to sort the records in a descending order, you can use the DESC keyword.
  • 8. SQL ORDER BY Syntax  SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC SELECT Person.Hobby FROM Person ORDER BY Person.Hobby; Hobby coins hiking stamps stamps
  • 9. SQL INSERT INTO  The INSERT INTO statement is used to insert a new row in a table. SQL INSERT INTO Syntax  It is possible to write the INSERT INTO statement in two forms.  The first form doesn't specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES (value1, value2, value3,...)  The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
  • 10. SQL INSERT INTO  INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')  INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob')
  • 11. SQL UPDATE The UPDATE statement is used to update existing records in a table. SQL UPDATE Syntax: UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example: UPDATE Persons SET Address=‘Nayabazar', City=‘Kathmandu' WHERE LastName=‘Ghimire' AND FirstName=‘Bishal'
  • 12. SQL UPDATE Warning  Be careful when updating records. If we had omitted the WHERE clause in the example above, like this: UPDATE Persons SET Address=‘Nayabazar', City=‘Kathmandu‘ What will be the result ?
  • 13. SQL DELETE  The DELETE statement is used to delete rows in a table. SQL DELETE Syntax DELETE FROM table_name WHERE some_column=some_value DELETE FROM Persons WHERE Address=‘Nayabazar', City=‘Kathmandu‘
  • 14. SQL TOP  The TOP clause is used to specify the number of records to return.  The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.  Note: Not all database systems support the TOP clause.  SQL Server Syntax  SELECT TOP number|percent column_name(s) FROM table_name
  • 15. SQL TOP  MySQL Syntax  SELECT column_name(s) FROM table_name LIMIT number  Oracle Syntax  SELECT column_name(s) FROM table_name WHERE ROWNUM <= number  SELECT * FROM Persons LIMIT 5
  • 16. SQL LIKE  The LIKE operator is used to search for a specified pattern in a column.  SQL LIKE Syntax  SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern
  • 17. SQL LIKE  To select the persons living in a city that starts with "s" from the table.  SELECT * FROM Persons WHERE City LIKE ‘k%'
  • 18. SQL Wildcards Wildcard Description % A substitute for zero or more characters _ A substitute for exactly one character [charlist] Any single character in charlist [^charlist]or [!charlist] Any single character not in charlist • SQL wildcards can substitute for one or more characters when searching for data in a database. • SQL wildcards must be used with the SQL LIKE operator.
  • 19. SQL IN  The IN operator allows you to specify multiple values in a WHERE clause.  SQL IN Syntax  SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...)
  • 20.  Example #1 Select * From Address Where FirstName IN ('Mary', 'Sam')  Example #2 SELECT * FROM Address WHERE FirstName = 'Mary' OR FirstName = 'Sam'
  • 21. SQL BETWEEN  The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.  SQL BETWEEN Syntax  SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2
  • 22.  Example:  SELECT * FROM suppliers WHERE supplier_id BETWEEN 5000 AND 5010;  SELECT * FROM suppliers WHERE supplier_id >= 5000 AND supplier_id <= 5010;
  • 23.  example:  SELECT * FROM orders WHERE order_date between to_date ('2013/01/01', 'yyyy/mm/dd') AND to_date ('2013/12/31', 'yyyy/mm/dd');  SELECT * FROM orders WHERE order_date >= to_date('2013/01/01', 'yyyy/mm/dd') AND order_date <= to_date('2013/12/31','yyyy/mm/dd');