SlideShare a Scribd company logo
1 of 19
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

What's hot (20)

Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
SQL
SQLSQL
SQL
 
Mysql
MysqlMysql
Mysql
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sql
 

Similar to SQL Tutorial - Basic Commands

sqltutorialbasiccommands-130310014513-phpapp01.pdf
sqltutorialbasiccommands-130310014513-phpapp01.pdfsqltutorialbasiccommands-130310014513-phpapp01.pdf
sqltutorialbasiccommands-130310014513-phpapp01.pdfpradeepvunnam2
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesAshwin Dinoriya
 
Veri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL DesteğiVeri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL DesteğiEmrah METE
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Databasesqlserver content
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Databasesqlserver content
 
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 - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table1keydata
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETEAbrar ali
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 

Similar to SQL Tutorial - Basic Commands (20)

sqltutorialbasiccommands-130310014513-phpapp01.pdf
sqltutorialbasiccommands-130310014513-phpapp01.pdfsqltutorialbasiccommands-130310014513-phpapp01.pdf
sqltutorialbasiccommands-130310014513-phpapp01.pdf
 
Sql
SqlSql
Sql
 
Sql
SqlSql
Sql
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
MySQL basics
MySQL basicsMySQL basics
MySQL basics
 
Hira
HiraHira
Hira
 
Veri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL DesteğiVeri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL Desteği
 
Retrieving Data From A Database
Retrieving Data From A DatabaseRetrieving Data From A Database
Retrieving Data From A Database
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Database
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Database
 
Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
intro for sql
intro for sql intro for sql
intro for sql
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 

Recently uploaded

{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationBoston Institute of Analytics
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknowmakika9823
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 

Recently uploaded (20)

{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 

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