SlideShare a Scribd company logo
2
Most read
10
Most read
13
Most read
SQL Tutorial
                                  Basic SQL Commands




© 2013 1keydata.com All Rights Reserved
Agenda
        • Database Basics
        • SQL Commands
             –   SELECT … FROM
             –   WHERE
             –   ORDER BY
             –   GROUP BY
             –   HAVING

© 2013 1keydata.com All Rights Reserved
Database Basics
       In a relational database, data is stored in tables.


                                          Tables




                   Database
© 2013 1keydata.com All Rights Reserved
Database Basics
        Each table consists of columns and rows. Each column is a
        field in a record, and there is a column name associated with
        each column.                                  Columns
                                Tables




                   Database
© 2013 1keydata.com All Rights Reserved
Database Basics
        Each row represents one record. When we say how many
        records we have, we are referring to the number of rows.
                                                     Columns
                             Tables



                                          Rows




                   Database
© 2013 1keydata.com All Rights Reserved
SELECT … FROM
        SQL is structured similar to the English language. The basic
        command for retrieving data from a database table is to
        SELECT data FROM a table. Not surprisingly, the keywords
        "SELECT" and "FROM" make up the core of a SQL
        statement.

        The syntax for “SELECT… FROM” is:
        SELECT “COLUMN_NAME”
        FROM “TABLE_NAME”

© 2013 1keydata.com All Rights Reserved
SELECT … FROM
        Different ways of selecting data:
        Select more than 1 column:
        SELECT “COLUMN_NAME_1”, “COLUMN_NAME_2”
        FROM “TABLE_NAME”


        Select all columns:               Select unique values:
        SELECT *                          SELECT DISTINCT “Column_Name”
        FROM “TABLE_NAME”                 FROM “TABLE_NAME”




© 2013 1keydata.com All Rights Reserved
WHERE
        Sometimes we want to retrieve only a subset of the data. In
        those cases, we use the “WHERE” keyword.

        The syntax for “WHERE” is:

        SELECT “COLUMN_NAME”
        FROM “TABLE_NAME”
        WHERE “CONDITION”
        CONDITION represents how we want the data to be filtered.

© 2013 1keydata.com All Rights Reserved
ORDER BY
        When we want to list the results in a particular order
        (ascending or descending), we use the ORDER BY keyword at
        the end of the SQL statement.

        The syntax for “ORDER BY” is:

        SELECT “COLUMN_NAME”
        FROM “TABLE_NAME”
        WHERE “CONDITION”
        ORDER BY “COLUMN_NAME” [ASC | DESC]
© 2013 1keydata.com All Rights Reserved
MATHEMATICAL FUNCTIONS

        SQL has built-in mathematical functions to allow us to
        perform mathematical operations on the data. Common
        mathematical functions include:
        • SUM
        • AVG
        • COUNT
        • MAX
        • MIN



© 2013 1keydata.com All Rights Reserved
GROUP BY

       To find the highest Sales_Amount across all stores, we use the
       MAX( ) function in the following SQL:

    SALES_HISTORY
     Date    Store    Sales_Amount
                                          SELECT MAX(Sales_Amount)
                                          FROM SALES_HISTORY;




© 2013 1keydata.com All Rights Reserved
GROUP BY

       To find the highest Sales_Amount for each store, we change
       the SELECT portion to include “Store”:

    SALES_HISTORY
     Date    Store    Sales_Amount
                                          SELECT Store, MAX(Sales_Amount)
                                          FROM SALES_HISTORY;




© 2013 1keydata.com All Rights Reserved
GROUP BY
       However, this SELECT statement by itself is not enough. To
       allow SQL to correctly calculate what we want, we need to use
       the GROUP BY keyword. In the following example, the Store
       column after GROUP BY tells SQL to apply the MAX
       function for each Store.
    SALES_HISTORY
     Date    Store    Sales_Amount
                                          SELECT Store, MAX(Sales_Amount)
                                          FROM SALES_HISTORY
                                          GROUP BY Store;


© 2013 1keydata.com All Rights Reserved
GROUP BY
        To summarize, the syntax for GROUP BY is as follows:


        SELECT “COLUMN_NAME_1”,
         FUNCTION(“COLUMN_NAME_2”)
        FROM “TABLE_NAME”
        WHERE “CONDITION”
        GROUP BY “COLUMN_NAME_1”


© 2013 1keydata.com All Rights Reserved
HAVING
        Previously we had talked about using the WHERE keyword to
        filter results.

        We cannot use WHERE to filter based on the result of a
        function, because we need to specify the filtering condition
        after SQL has calculated the function, and consequently any
        filtering condition based on the function needs to be specified
        after the GROUP BY phrase. So we cannot use the WHERE
        keyword because it is always used before GROUP BY.

        HAVING is used to filter based on the result of a function.

© 2013 1keydata.com All Rights Reserved
HAVING
        The syntax for HAVING is as follows:

        SELECT “COLUMN_NAME_1”,
         FUNCTION(“COLUMN_NAME_2”)
        FROM “TABLE_NAME”
        GROUP BY “COLUMN_NAME_1”
        HAVING (CONDITION based on
         FUNCTION)

© 2013 1keydata.com All Rights Reserved
HAVING
       Using the SALES_HISTORY table we had earlier. If we want
       to sum the sales amount for each store, but only want to see
       results for stores with total sales amount greater than 100, we
       use the following SQL:
    SALES_HISTORY
     Date    Store    Sales_Amount         SELECT Store, SUM(Sales_Amount)
                                           FROM SALES_HISTORY
                                           GROUP BY Store
                                           HAVING SUM(Sales_Amount) > 100;

© 2013 1keydata.com All Rights Reserved
Order of SQL Commands
        A SELECT statement has the following order:
        • SELECT … FROM
        • WHERE
        • GROUP BY
        • HAVING
        • ORDER BY


© 2013 1keydata.com All Rights Reserved
1Keydata SQL Tutorial
               http://www.1keydata.com/sql/sql.html




© 2013 1keydata.com All Rights Reserved

More Related Content

PPTX
Chapter 1 introduction to sql server
PPTX
Introduction to SQL
PDF
Sql tutorial
PPTX
SQL - Structured query language introduction
PDF
SQL Overview
PPT
Introduction to-sql
PPTX
MS ACCESS PPT.pptx
PPT
Chapter 1 introduction to sql server
Introduction to SQL
Sql tutorial
SQL - Structured query language introduction
SQL Overview
Introduction to-sql
MS ACCESS PPT.pptx

What's hot (20)

PPTX
Sql commands
DOC
PPTX
SQL Basics
PPTX
ODP
Ms sql-server
PPTX
introdution to SQL and SQL functions
PDF
Sql Basics | Edureka
PPTX
PLSQL Tutorial
PPTX
SQL Commands
PPTX
Presentation slides of Sequence Query Language (SQL)
PPTX
SQL - DML and DDL Commands
PPT
Sql ppt
PPTX
Sql Constraints
ODP
PPTX
SQL Queries Information
PPT
SQL Queries
PPT
MySQL and its basic commands
PPTX
Sql(structured query language)
PPT
Introduction to structured query language (sql)
PPTX
SQL(DDL & DML)
Sql commands
SQL Basics
Ms sql-server
introdution to SQL and SQL functions
Sql Basics | Edureka
PLSQL Tutorial
SQL Commands
Presentation slides of Sequence Query Language (SQL)
SQL - DML and DDL Commands
Sql ppt
Sql Constraints
SQL Queries Information
SQL Queries
MySQL and its basic commands
Sql(structured query language)
Introduction to structured query language (sql)
SQL(DDL & DML)
Ad

Similar to SQL Tutorial - Basic Commands (20)

PDF
sqltutorialbasiccommands-130310014513-phpapp01.pdf
DOC
DOC
PPTX
Subqueries, Backups, Users and Privileges
PDF
Oracle SQL Basics
DOC
Oracle notes
PPTX
MySQL basics
PPTX
SQL OVERVIEW for a new introduced student.pptx
PPTX
PPTX
SQL_for_Azure_Data_Engineer_basic_to_adv
PPTX
Veri Ambarları için Oracle'ın Analitik SQL Desteği
PPTX
Retrieving Data From A Database
PPTX
MS SQLSERVER:Retrieving Data From A Database
PPTX
MS SQL SERVER: Retrieving Data From A Database
PDF
Sql wksht-3
PPT
PPT
Beg sql
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PPTX
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
PPT
SQL.ppt
sqltutorialbasiccommands-130310014513-phpapp01.pdf
Subqueries, Backups, Users and Privileges
Oracle SQL Basics
Oracle notes
MySQL basics
SQL OVERVIEW for a new introduced student.pptx
SQL_for_Azure_Data_Engineer_basic_to_adv
Veri Ambarları için Oracle'ın Analitik SQL Desteği
Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Database
Sql wksht-3
Beg sql
Chapter – 6 SQL Lab Tutorial.pdf
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
SQL.ppt
Ad

Recently uploaded (20)

PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
batch data Retailer Data management Project.pptx
PDF
Report The-State-of-AIOps 20232032 3.pdf
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Computer network topology notes for revision
PDF
Foundation of Data Science unit number two notes
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PDF
Taxes Foundatisdcsdcsdon Certificate.pdf
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Linux OS guide to know, operate. Linux Filesystem, command, users and system
.pdf is not working space design for the following data for the following dat...
Business Ppt On Nestle.pptx huunnnhhgfvu
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
batch data Retailer Data management Project.pptx
Report The-State-of-AIOps 20232032 3.pdf
oil_refinery_comprehensive_20250804084928 (1).pptx
IB Computer Science - Internal Assessment.pptx
Quality review (1)_presentation of this 21
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
1_Introduction to advance data techniques.pptx
Business Acumen Training GuidePresentation.pptx
Computer network topology notes for revision
Foundation of Data Science unit number two notes
Moving the Public Sector (Government) to a Digital Adoption
Taxes Foundatisdcsdcsdon Certificate.pdf
Reliability_Chapter_ presentation 1221.5784
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Linux OS guide to know, operate. Linux Filesystem, command, users and system

SQL Tutorial - Basic Commands

  • 1. SQL Tutorial Basic SQL Commands © 2013 1keydata.com All Rights Reserved
  • 2. Agenda • Database Basics • SQL Commands – SELECT … FROM – WHERE – ORDER BY – GROUP BY – HAVING © 2013 1keydata.com All Rights Reserved
  • 3. Database Basics In a relational database, data is stored in tables. Tables Database © 2013 1keydata.com All Rights Reserved
  • 4. Database Basics Each table consists of columns and rows. Each column is a field in a record, and there is a column name associated with each column. Columns Tables Database © 2013 1keydata.com All Rights Reserved
  • 5. Database Basics Each row represents one record. When we say how many records we have, we are referring to the number of rows. Columns Tables Rows Database © 2013 1keydata.com All Rights Reserved
  • 6. SELECT … FROM SQL is structured similar to the English language. The basic command for retrieving data from a database table is to SELECT data FROM a table. Not surprisingly, the keywords "SELECT" and "FROM" make up the core of a SQL statement. The syntax for “SELECT… FROM” is: SELECT “COLUMN_NAME” FROM “TABLE_NAME” © 2013 1keydata.com All Rights Reserved
  • 7. SELECT … FROM Different ways of selecting data: Select more than 1 column: SELECT “COLUMN_NAME_1”, “COLUMN_NAME_2” FROM “TABLE_NAME” Select all columns: Select unique values: SELECT * SELECT DISTINCT “Column_Name” FROM “TABLE_NAME” FROM “TABLE_NAME” © 2013 1keydata.com All Rights Reserved
  • 8. WHERE Sometimes we want to retrieve only a subset of the data. In those cases, we use the “WHERE” keyword. The syntax for “WHERE” is: SELECT “COLUMN_NAME” FROM “TABLE_NAME” WHERE “CONDITION” CONDITION represents how we want the data to be filtered. © 2013 1keydata.com All Rights Reserved
  • 9. ORDER BY When we want to list the results in a particular order (ascending or descending), we use the ORDER BY keyword at the end of the SQL statement. The syntax for “ORDER BY” is: SELECT “COLUMN_NAME” FROM “TABLE_NAME” WHERE “CONDITION” ORDER BY “COLUMN_NAME” [ASC | DESC] © 2013 1keydata.com All Rights Reserved
  • 10. MATHEMATICAL FUNCTIONS SQL has built-in mathematical functions to allow us to perform mathematical operations on the data. Common mathematical functions include: • SUM • AVG • COUNT • MAX • MIN © 2013 1keydata.com All Rights Reserved
  • 11. GROUP BY To find the highest Sales_Amount across all stores, we use the MAX( ) function in the following SQL: SALES_HISTORY Date Store Sales_Amount SELECT MAX(Sales_Amount) FROM SALES_HISTORY; © 2013 1keydata.com All Rights Reserved
  • 12. GROUP BY To find the highest Sales_Amount for each store, we change the SELECT portion to include “Store”: SALES_HISTORY Date Store Sales_Amount SELECT Store, MAX(Sales_Amount) FROM SALES_HISTORY; © 2013 1keydata.com All Rights Reserved
  • 13. GROUP BY However, this SELECT statement by itself is not enough. To allow SQL to correctly calculate what we want, we need to use the GROUP BY keyword. In the following example, the Store column after GROUP BY tells SQL to apply the MAX function for each Store. SALES_HISTORY Date Store Sales_Amount SELECT Store, MAX(Sales_Amount) FROM SALES_HISTORY GROUP BY Store; © 2013 1keydata.com All Rights Reserved
  • 14. GROUP BY To summarize, the syntax for GROUP BY is as follows: SELECT “COLUMN_NAME_1”, FUNCTION(“COLUMN_NAME_2”) FROM “TABLE_NAME” WHERE “CONDITION” GROUP BY “COLUMN_NAME_1” © 2013 1keydata.com All Rights Reserved
  • 15. HAVING Previously we had talked about using the WHERE keyword to filter results. We cannot use WHERE to filter based on the result of a function, because we need to specify the filtering condition after SQL has calculated the function, and consequently any filtering condition based on the function needs to be specified after the GROUP BY phrase. So we cannot use the WHERE keyword because it is always used before GROUP BY. HAVING is used to filter based on the result of a function. © 2013 1keydata.com All Rights Reserved
  • 16. HAVING The syntax for HAVING is as follows: SELECT “COLUMN_NAME_1”, FUNCTION(“COLUMN_NAME_2”) FROM “TABLE_NAME” GROUP BY “COLUMN_NAME_1” HAVING (CONDITION based on FUNCTION) © 2013 1keydata.com All Rights Reserved
  • 17. HAVING Using the SALES_HISTORY table we had earlier. If we want to sum the sales amount for each store, but only want to see results for stores with total sales amount greater than 100, we use the following SQL: SALES_HISTORY Date Store Sales_Amount SELECT Store, SUM(Sales_Amount) FROM SALES_HISTORY GROUP BY Store HAVING SUM(Sales_Amount) > 100; © 2013 1keydata.com All Rights Reserved
  • 18. Order of SQL Commands A SELECT statement has the following order: • SELECT … FROM • WHERE • GROUP BY • HAVING • ORDER BY © 2013 1keydata.com All Rights Reserved
  • 19. 1Keydata SQL Tutorial http://www.1keydata.com/sql/sql.html © 2013 1keydata.com All Rights Reserved