SQL Commands

  • View
    24

  • Download
    3

Embed Size (px)

DESCRIPTION

Pocket guide about SQL Commands

Text of SQL Commands

  • 1 page of [20]

    BASIC SQL COMMANDS ( Structured Query Language )

  • 2 page of [20]

    CONTENTS

    1. SQL DATABASE TABLE ...................................................................................... 03

    2. SQL SELECT............................................................................................................ 03

    3. SQL SELECT INTO ................................................................................................ 03

    4. SQL DISTINCT ........................................................................................................ 04

    5. SQL WHERE ............................................................................................................ 04-05

    6. SQL LIKE .................................................................................................................. 06

    7. SQL INSERT INTO ................................................................................................. 07-08

    8. SQL UPDATE ........................................................................................................... 08

    9. SQL DELETE ........................................................................................................... 09

    10. SQL ORDER BY ...................................................................................................... 09-10

    11. SQL OR & AND ....................................................................................................... 10-11

    12. SQL IN ....................................................................................................................... 11-12

    13. SQL BETWEEN ....................................................................................................... 12-13

    14. SQL ALIASES .......................................................................................................... 13

    15. SQL COUNT ............................................................................................................. 13-14

    16. SQL MAX .................................................................................................................. 14

    17. SQL MIN ................................................................................................................... 14

    18. SQL AVG ................................................................................................................... 14-15

    19. SQL SUM ................................................................................................................... 15

    20. SQL GROUP BY ...................................................................................................... 16-17

    21. SQL HAVING ........................................................................................................... 17

    22. SQL JOIN .................................................................................................................. 18-20

  • 3 page of [20]

    SQL Commands

    SQL Database Table

    Table: Customers

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545

    Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    SELECT

    The SQL SELECT statement is used to select data from a SQL database table.

    Please have a look at the general SQL SELECT syntax:

    SELECT Column1, Column2, Column3,

    FROM Table1

    The list of column names after the SQL SELECT command determines which columns you want to

    be returned in your result set.

    SELECT *

    FROM Table1

    When the list of columns following the SELECT SQL command is replaced with asterix (*) all table

    columns are returned.

    SELECT INTO

    The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it

    to a different table at the same time.

    SELECT Column1, Column2, Column3,

    INTO Table2

    FROM Table1

    If we want to make an exact copy of the data in our Customers table, we need the following SQL

    SELECT INTO statement:

    SELECT *

    INTO Customers_copy

    FROM Customers

  • 4 page of [20]

    DISTINCT

    The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset

    with unique entries for certain database table column.

    We will use our Customers database table to illustrate the usage of SQL DISTINCT.

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545

    Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    For example if we want to select all distinct surnames from our Customers table, we will use the

    following SQL DISTINCT statement:

    SELECT DISTINCT LastName

    FROM Customers

    LastName

    Smith

    Goldfish

    Brown

    WHERE

    The SQL WHERE clause is used to select data conditionally, by adding it to already existing SQL

    SELECT query. We are going to use the Customers table from the previous chapter, to illustrate the

    use of the SQL WHERE command.

    Table: Customers

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545

    Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    If we want to select all customers from our database table, having last name 'Smith' we need to use the

    following SQL syntax:

    SELECT *

    FROM Customers

    WHERE LastName = 'Smith'

    The result of the SQL expression above will be the following:

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

  • 5 page of [20]

    In this simple SQL query we used the "=" (Equal) operator in our WHERE criteria:

    LastName = 'Smith'

    But we can use any of the following comparison operators in conjunction with the SQL WHERE

    clause:

    (Not Equal)

    SELECT *

    FROM Customers

    WHERE LastName 'Smith'

    > (Greater than)

    SELECT *

    FROM Customers

    WHERE DOB > '1/1/1970'

    >= (Greater or Equal)

    SELECT *

    FROM Customers

    WHERE DOB >= '1/1/1970'

    < (Less than)

    SELECT *

    FROM Customers

    WHERE DOB < '1/1/1970'

  • 6 page of [20]

    LIKE

    We will use the Customers table to illustrate the SQL LIKE clause usage:

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545

    Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    The SQL LIKE clause is very useful when you want to specify a search condition within your SQL

    WHERE clause, based on a part of a column contents. For example if you want to select all customers

    having FirstName starting with 'J' you need to use the following SQL statement:

    SELECT *

    FROM Customers

    WHERE FirstName LIKE 'J%'

    Here is the result of the SQL statement above:

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    If you want to select all Customers with phone numbers starting with '416' you will use this SQL

    expression:

    SELECT *

    FROM Customers

    WHERE Phone LIKE '416%'

    The '%' is a so called wildcard character and represents any string in our pattern.

    You can put the wildcard anywhere in the string following the SQL LIKE clause and you can put as

    many wildcards as you like too.

    Note that different databases use different characters as wildcard characters, for example '%' is a

    wildcard character for MS SQL Server representing any string, and '*' is the corresponding wildcard

    character used in MS Access.

    Another wildcard character is '_' representing any single character.

    The '[]' specifies a range of characters. Have a look at the following SQL statement:

    SELECT *

    FROM Customers

    WHERE Phone LIKE '[4-6]_6%'

  • 7 page of [20]

    This SQL expression will return all customers satisfying the following conditions:

    The Phone column starts with a digit between 4 and 6 ([4-6])

    Second character in the Phone column can be anything (_)

    The third character in the Phone column is 6 (6)

    The remainder of the Phone column can be any character string (%)

    FirstName LastName Email DOB Phone

    John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222

    Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232

    James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

    INSERT INTO

    The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new

    row into the database table.

    The first syntax form of the INSERT INTO SQL clause doesn't specify the column names where the

    data will be inserted, but just their values:

    INSERT INTO Table1

    VALUES (value1, value2, value3)

    The second form of the SQ