SlideShare a Scribd company logo
SQL – Structured Query
       Language




                 By Pratik Rathod
DATABASE
•   Organized collection of data
•   DBMS – Data Base Management System
•   Ex. Oracle, Microsoft SQL Server, IBM DB2
•   2 types of DBMS
    – Flat-file database
    – Relational database
SQL
• Structured Query Language -> RDBMS
• A little History
  – Developed by Donald D. Chamberlin & Raymond F.
    Boyce at IBM in 1970
  – Initially it was named as SEQUEL (Structured
    English Query Language)
• Queries – most commonly used operation & can be
  done by SELECT
• SQL is not case sensitive. 
SQL for…
•   Execute queries against database
•   Retrieve data from database
•   Insert, Update, Delete records in database
•   Create new tables in database
•   Set permissions on tables
•   “Persons” table in database

    ID         Firstname   Lastname   Address    City
              1Pratik      Rathod     memnagar   Ahmedabad
              2Pranav      Agarwal    Paldi      Ahmedabad
              3Karan       Koria      Girnar     Junagadh
SQL Statements
• Not case sensitive
• Semi columns
• SQL - DML – Data manipulation language           SQL

       - DDL - Data definition language
                                        DML                  DDL

                                                            CREATE
                                       SELECT              DATABASE
                                       UPDATE            ALTER DATABASE
                                       DELETE             CREATE TABLE
                                     INSERT INTO          ALTER TABLE
                                                          DROP TBALE
                                                          CREATE INDEX
                                                          DROP INDEX
SQL Syntax & Statements
• SELECT
Syntax : SELECT column_name(s) FROM table_name
Examples:
1) SELECT LastName,FirstName FROM Persons
2) SELECT * FROM Persons (* is a quick way to select all the columns)

Where our selected columns stored????...

           ID            Firstname      Lastname      Address       City
                        1Pratik         Rathod        memnagar      Ahmedabad
                        2Pranav         Agarwal       Paldi         Ahmedabad
                        3Karan          Koria         Girnar        Junagadh
SQL Syntax & Statements
• SELECT DISTINCT                        • Where (clause)
Syntax :                                 Syntax :
SELECT DISTINCT column_name(s)           SELECT column_name(s) FROM table_name
FROM table_name                             WHERE column_name operator value

Example:                                 Example:
SELECT DISTINCT City FROM Persons        SELECT * FROM Persons WHERE
                                            City=‘Ahmedabad‘
City

Ahmedabad
                         ID          Firstname Lastname   Address  City
Junagadh                            1Pratik    Rathod     memnagar Ahmedabad
                                    2Pranav    Agarwal    Paldi    Ahmedabad
SQL Syntax & Statements
• Operators in WHERE         • CREATE TABLE
  = Equals                   Syntax :
  <> or != Not equal         CREATE TABLE table_name
  > Greater than                 (
  < Less than                    column_name1 data_type,
                                 column_name2 data_type,
  >= Greater than or equal       column_name3 data_type,
  <= Less than or equal          ....
                                 )

                             Example:
                             CREATE TABLE Persons
                                (
                                Id int,
                                LastName varchar(255),
                                FirstName varchar(255),
                                Address varchar(255),
                                City varchar(255)
                                )
SQL Syntax & Statements
• AND (Operator)               • AND & OR together
Ex.                            Ex.
SELECT * FROM Persons          SELECT * FROM Persons WHERE
WHERE FirstName=‘Pranav‘ AND       LastName=‘Agarwal'
    LastName=‘Agarwal‘             AND (FirstName=‘Pranav' OR
                                   FirstName=‘Pratik')

• OR (Operator)
Ex.
SELECT * FROM Persons
WHERE FirstName=‘Pranav'
    OR FirstName=‘Pratik'
SQL Syntax & Statements
• ORDER BY (Clause)                    • INSERT INTO
Syntax : SELECT column_name(s)
    FROM table_name
                                           – To insert a new row
    ORDER BY column_name(s) ASC|DESC   Syntax : INSERT INTO table_name
                                          (column1, column2, column3,...)
Ex.                                       VALUES (value1, value2, value3,...)
1) SELECT * FROM Persons               Ex.
    ORDER BY LastName
                                       1) INSERT INTO Persons
2) SELECT * FROM Persons                   VALUES
    ORDER BY LastName DESC
                                           (4,‘Naman', ‘Shah', ‘gandhinagar', ‘g
                                           andhinagar')

                                       2) INSERT INTO Persons
                                           (P_Id, LastName, FirstName) VALUES
                                           (5, ‘Amin', ‘Jay')
SQL Syntax & Statements
• UPDATE                                    • DELETE
Syntax : UPDATE table_name                  Syntax : DELETE FROM table_name
    SET
    column1=value, column2=value2,...
    WHERE some_column=some_value            Example:
Ex.                                         1) DELETE * FROM Persons
                                            2) DELETE FROM Persons
UPDATE Persons                                 WHERE Lastname = ‘Rathod’
SET Address=‘Sector21',City=‘Gandhinagar'
   WHERE LastName=‘Shah' AND
   FirstName=‘Naman’                        CAUTION:
                                            DELETE cannot be undo.
SQL elements
Where to write this things??...
•   Install java platform with JDBC API
•   Install a driver
•   Install DBMS (if u don’t have any)
•   Set up Database
•   Load drivers (In java program)
•   Make connection (In java program)
•   Write SQL statements in your program.
    – E.g.
    Statement stmt = con.createStatement();
                       (
    stmt.executeUpdate "INSERT INTO COFFEES " +
                     "VALUES ('Colombian', 101, 7.99, 0, 0)");
Thank you

More Related Content

DOC
Sql plsql
PDF
1 data types
PDF
GQL CheatSheet 1.1
PDF
GQL cheat sheet latest
PDF
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
DOCX
supporting t-sql scripts for Heap vs clustered table
KEY
Postgres rules
PDF
Sql server difference faqs- 3
Sql plsql
1 data types
GQL CheatSheet 1.1
GQL cheat sheet latest
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
supporting t-sql scripts for Heap vs clustered table
Postgres rules
Sql server difference faqs- 3

What's hot (20)

PPT
Oracle Sql & PLSQL Complete guide
PDF
Building a SQL Database that Works
PDF
Java for beginners
PDF
Developing Applications with MySQL and Java for beginners
PPT
14. Defining Classes
PDF
C# Advanced L03-XML+LINQ to XML
PDF
4 execution plans
PPTX
MS SQL Database basic
ODP
Scala Reflection & Runtime MetaProgramming
PDF
The Ring programming language version 1.10 book - Part 47 of 212
PDF
Solid and Sustainable Development in Scala
PPTX
Scrap your query boilerplate with specql
PDF
The Ring programming language version 1.5.3 book - Part 37 of 184
KEY
PPT
Advanced php
ODP
Slickdemo
PDF
Be Smart, Constrain Your Types to Free Your Brain!
PPTX
PDF
Sql functions
PDF
ZIO Prelude - ZIO World 2021
Oracle Sql & PLSQL Complete guide
Building a SQL Database that Works
Java for beginners
Developing Applications with MySQL and Java for beginners
14. Defining Classes
C# Advanced L03-XML+LINQ to XML
4 execution plans
MS SQL Database basic
Scala Reflection & Runtime MetaProgramming
The Ring programming language version 1.10 book - Part 47 of 212
Solid and Sustainable Development in Scala
Scrap your query boilerplate with specql
The Ring programming language version 1.5.3 book - Part 37 of 184
Advanced php
Slickdemo
Be Smart, Constrain Your Types to Free Your Brain!
Sql functions
ZIO Prelude - ZIO World 2021
Ad

Viewers also liked (11)

PPT
Lecture 4 software process model (2)
PPTX
Relational Data Model Introduction
PDF
Introduction: Databases and Database Users
PDF
C++ OOPS Concept
PPT
Rdbms
PPTX
All data models in dbms
PPTX
Software Engg. process models
PPT
Sql ppt
PPT
Different data models
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
PDF
LinkedIn SlideShare: Knowledge, Well-Presented
Lecture 4 software process model (2)
Relational Data Model Introduction
Introduction: Databases and Database Users
C++ OOPS Concept
Rdbms
All data models in dbms
Software Engg. process models
Sql ppt
Different data models
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
LinkedIn SlideShare: Knowledge, Well-Presented
Ad

Similar to Sql (20)

PDF
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
PDF
Oracle Material.pdf
PPTX
Lesson-02 (1).pptx
PPT
Db1 lecture4
PPT
Sql
PPT
MySql slides (ppt)
PPTX
PDF
Harder Faster Stronger
PPT
MySQL Database System Hiep Dinh
PPT
mysqlHiep.ppt
PDF
New methods for exploiting ORM injections in Java applications
KEY
No SQL, No problem - using MongoDB in Ruby
PPTX
Dbms &amp; oracle
PPTX
lab#1,2,3.pptx
PPT
dbs class 7.ppt
PPTX
Advanced SQL Webinar
PDF
Integrity constraints in dbms
ODP
Ms sql-server
PPTX
Chapter 1 introduction to sql server
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Oracle Material.pdf
Lesson-02 (1).pptx
Db1 lecture4
Sql
MySql slides (ppt)
Harder Faster Stronger
MySQL Database System Hiep Dinh
mysqlHiep.ppt
New methods for exploiting ORM injections in Java applications
No SQL, No problem - using MongoDB in Ruby
Dbms &amp; oracle
lab#1,2,3.pptx
dbs class 7.ppt
Advanced SQL Webinar
Integrity constraints in dbms
Ms sql-server
Chapter 1 introduction to sql server

Sql

  • 1. SQL – Structured Query Language By Pratik Rathod
  • 2. DATABASE • Organized collection of data • DBMS – Data Base Management System • Ex. Oracle, Microsoft SQL Server, IBM DB2 • 2 types of DBMS – Flat-file database – Relational database
  • 3. SQL • Structured Query Language -> RDBMS • A little History – Developed by Donald D. Chamberlin & Raymond F. Boyce at IBM in 1970 – Initially it was named as SEQUEL (Structured English Query Language) • Queries – most commonly used operation & can be done by SELECT • SQL is not case sensitive. 
  • 4. SQL for… • Execute queries against database • Retrieve data from database • Insert, Update, Delete records in database • Create new tables in database • Set permissions on tables • “Persons” table in database ID Firstname Lastname Address City 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad 3Karan Koria Girnar Junagadh
  • 5. SQL Statements • Not case sensitive • Semi columns • SQL - DML – Data manipulation language SQL - DDL - Data definition language DML DDL CREATE SELECT DATABASE UPDATE ALTER DATABASE DELETE CREATE TABLE INSERT INTO ALTER TABLE DROP TBALE CREATE INDEX DROP INDEX
  • 6. SQL Syntax & Statements • SELECT Syntax : SELECT column_name(s) FROM table_name Examples: 1) SELECT LastName,FirstName FROM Persons 2) SELECT * FROM Persons (* is a quick way to select all the columns) Where our selected columns stored????... ID Firstname Lastname Address City 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad 3Karan Koria Girnar Junagadh
  • 7. SQL Syntax & Statements • SELECT DISTINCT • Where (clause) Syntax : Syntax : SELECT DISTINCT column_name(s) SELECT column_name(s) FROM table_name FROM table_name WHERE column_name operator value Example: Example: SELECT DISTINCT City FROM Persons SELECT * FROM Persons WHERE City=‘Ahmedabad‘ City Ahmedabad ID Firstname Lastname Address City Junagadh 1Pratik Rathod memnagar Ahmedabad 2Pranav Agarwal Paldi Ahmedabad
  • 8. SQL Syntax & Statements • Operators in WHERE • CREATE TABLE = Equals Syntax : <> or != Not equal CREATE TABLE table_name > Greater than ( < Less than column_name1 data_type, column_name2 data_type, >= Greater than or equal column_name3 data_type, <= Less than or equal .... ) Example: CREATE TABLE Persons ( Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )
  • 9. SQL Syntax & Statements • AND (Operator) • AND & OR together Ex. Ex. SELECT * FROM Persons SELECT * FROM Persons WHERE WHERE FirstName=‘Pranav‘ AND LastName=‘Agarwal' LastName=‘Agarwal‘ AND (FirstName=‘Pranav' OR FirstName=‘Pratik') • OR (Operator) Ex. SELECT * FROM Persons WHERE FirstName=‘Pranav' OR FirstName=‘Pratik'
  • 10. SQL Syntax & Statements • ORDER BY (Clause) • INSERT INTO Syntax : SELECT column_name(s) FROM table_name – To insert a new row ORDER BY column_name(s) ASC|DESC Syntax : INSERT INTO table_name (column1, column2, column3,...) Ex. VALUES (value1, value2, value3,...) 1) SELECT * FROM Persons Ex. ORDER BY LastName 1) INSERT INTO Persons 2) SELECT * FROM Persons VALUES ORDER BY LastName DESC (4,‘Naman', ‘Shah', ‘gandhinagar', ‘g andhinagar') 2) INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, ‘Amin', ‘Jay')
  • 11. SQL Syntax & Statements • UPDATE • DELETE Syntax : UPDATE table_name Syntax : DELETE FROM table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example: Ex. 1) DELETE * FROM Persons 2) DELETE FROM Persons UPDATE Persons WHERE Lastname = ‘Rathod’ SET Address=‘Sector21',City=‘Gandhinagar' WHERE LastName=‘Shah' AND FirstName=‘Naman’ CAUTION: DELETE cannot be undo.
  • 13. Where to write this things??... • Install java platform with JDBC API • Install a driver • Install DBMS (if u don’t have any) • Set up Database • Load drivers (In java program) • Make connection (In java program) • Write SQL statements in your program. – E.g. Statement stmt = con.createStatement(); ( stmt.executeUpdate "INSERT INTO COFFEES " + "VALUES ('Colombian', 101, 7.99, 0, 0)");