SlideShare ist ein Scribd-Unternehmen logo
1 von 22
SQL Select
. . . the very basics

                        Bob Litsinger
                        bob.litsinger@gmail.com
Introduction
                             This slide set covers basic select
                              statements against a single table.
                             It does not cover:
                                 Joins and other table to table interactions.
Bob Litsinger
                                 Using sub-queries in a primary query.
bob.litsinger@gmail.com
                                 The use of the many functions available in SQL.
                             There are a few examples of functions, and
                              an additional topics list at the end.
Contents
                                                      SQL Select . . . the very basics
                          Introduction                             2   More on ORDER BY               13
                          Contents                                 3   Having                         14
                          English like                             4   Operators                      15
                          Simple Select                            5   Qualifiers for WHERE           16
                          Specifying the fields                    6   More on qualifiers for WHERE   17
                          Introducing WHERE clauses                7   BETWEEN with datetime type     18
Bob Litsinger
bob.litsinger@gmail.com   Exploring information                    8   Wildcards with LIKE            19
                          Count – How Many Products                9   ALIASES, CAST and CONVERT      20
                          A few aggregations                      10   Other Capabilities             21
                          Establishing order                      11   Queries Unleashed              22
                          More on GROUP BY                        12
English like
                             Basic SQL is very intuitive for users
                              because a request is English like:


Bob Litsinger
bob.litsinger@gmail.com




                                 . . . but using this structure, we can
                                  ask complicated questions.
Simple Select
                             I wonder what types of products
                              Adventure              * Will show us all the
                                                            details (called fields) about
                                                            the Categories


Bob Litsinger
bob.litsinger@gmail.com




                                 To much information? You can get
                                  just the ID and Name by being more
                                  specific.
Specify the fields
                             So, let’s get the Category ID, Name
                              and Manager:


Bob Litsinger
bob.litsinger@gmail.com




                                 Using the Category ID for Bikes
                                  (1), the types of bikes can be found
                                  in the Subcategory table.
Introducing WHERE clauses
                             To limit the Sub Categories to bikes, a
                              WHERE clause is added:


Bob Litsinger
bob.litsinger@gmail.com
Exploring information
                             Looking only at Road Bikes in the
                              Product line up will enable us to
                              explore some data in other ways.

Bob Litsinger
bob.litsinger@gmail.com



                             Gives us a list of 43 bikes



                                                            More are listed
Count - How Many Products
                             The count(*) function provides a
                              simple count of the bikes
                                                                 This assigns a name to a column. If the
                                                                 name has spaces, keywords or some
                                                                 other characters it requires brackets.


Bob Litsinger                            All Products
bob.litsinger@gmail.com


                                                                 The word AS is optional, but makes
                                                                 things clearer. If there are no spaces or
                                                                 invalid characters, brackets are not
                                                                 needed.

                                       Just Road Bikes


                                                         Introducing column labels
A few aggregations.
                          Aggregations allow                I can add a label to each row. It is listed as a query
                                                                                          item and is in quotes.
                          analysis of numeric values.
                               Simple count of number of
                           Road 52 cm road bikes by color
                                 Finds the minimum, i.e.
                                 lowest, value

                          Calculates the average value.

                              Finds the maximum, i.e.
Bob Litsinger                 highest, value
bob.litsinger@gmail.com
                          To calculate the                                      Notice the WHERE clause can
                          aggregates the                                        include many conditions
                          query needs to
                          know how the
                          listing is grouped.



                          A variation:                      Finds the distinct number of colors of road bikes
                                                            (for all sizes).
Establishing order
                          SUM() is the most frequently   This is a function that returns the number of the
                                                          month. Functions will be discussed briefly later.
                          used aggregate.




                          Notice something else new.
                          An ORDER BY clause was
                          added to put the months in
Bob Litsinger
bob.litsinger@gmail.com   order by occurrence.
More on GROUP BY


                                                     If the GROUP BY
                                                     clause is left off


                              If the GROUP BY
                              clause is incomplete
Bob Litsinger
bob.litsinger@gmail.com




                                                                Color is not listed!



                                                             However, if the aggregate
                                                             applies only to the entire
                                                             query with no breakdown
                                                             by any category, then a
                                                             GROUP BY is not needed.
More on ORDER BY
                             It is always the last cause.
                              Sorting is the last step for the
                              SQL engine, and the request
                              for it is placed last.

                             Can be against multiple
                              fields, and any field order.
Bob Litsinger
bob.litsinger@gmail.com

                             Does not need to agree with
                              the fields or field order in
                              GROUP BY

                             Is the only place in a query
                              script where a field alias can
                              be used.
HAVING
                          Works like a “WHERE” clause but for aggregations




Bob Litsinger
bob.litsinger@gmail.com
Operators
                            +   Addition and Concatenation
                                                              Used in WHERE clause . . .
                            -   Subtraction
                            *   Multiplication
                            /   Division
                            =   Equals
                           <>   Does Not Equal
                           !=   Does Not Equal
Bob Litsinger
bob.litsinger@gmail.com    >=   Greater Than or Equal To
                           <=   Less Than or Equal To
                            >   Greater Than
                            <   Less Than
                           %    Mod operator (also used as wildcard)

                                     . . . or to calculate a value.
Qualifiers for WHERE
                          There are some special operators that can be used in the WHERE
                          clause.
                                              These can be used with NOT.




Bob Litsinger
bob.litsinger@gmail.com




                          For LIKE there are even some
                          special wildcard characters.
More on qualifiers for
                          WHERE


                             IN and NOT IN


Bob Litsinger
bob.litsinger@gmail.com
                          Be sure your range is “inclusive” when you use between.

                                                            Using a single beginning letter
                                                            will not get the Subcategories
                                                            that begin with “M.” To get all
                                                            these add zzz:


                          With datetime data
                          type
BETWEEN with datetime type
                          The datetime data type has a time with it. If the data actually records
                          time between will leave out records if you rely on implicit text to
                          datetime conversion. This small test tables shows the problem.




Bob Litsinger
bob.litsinger@gmail.com


                          These two queries solve the problem:

                                                                     Both get all the records.
Wildcards with LIKE



Bob Litsinger
bob.litsinger@gmail.com
Aliases, Convert and Cast
                             Some ways to make things more readable are
                              shown in the slide
                                 Many of the slides use field aliases to provide user
                                  friendly column labels.
                                 CAST and CONVERT are both used in places. These make
                                  query results more presentable.
Bob Litsinger
bob.litsinger@gmail.com


                             There are other options (like STR) and
                              CONVERT/CAST setting that you can use. There
                              are specific settings in CONVERT for date formats.
Other Capabilities
                             Advanced aggregations like CUBE, ROLLUP and
                              PIVOT
                             Specific Math, String, Date, Text, Image and other
                              Functions

Bob Litsinger                Program controls like IF, WHILE, CASE, etc.
bob.litsinger@gmail.com


                             Stored Procedures and Functions
                             System Functions and Procedures
                             Triggers and Database Management
Queries Unleashed
                          To really tapped the power of a relational
                          database by using multiple tables.

                             Use tables together with JOIN, UNION, EXCEPT
Bob Litsinger
                              and MERGE.
bob.litsinger@gmail.com


                             Use sub-queries to help the extend the power of a
                              primary query.

                                          Check out other existing and future slide shows
                                          for examples of other SQL functions, capabilities
                                          and scripting.

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Sql Basic Selects

  • 1. SQL Select . . . the very basics Bob Litsinger bob.litsinger@gmail.com
  • 2. Introduction  This slide set covers basic select statements against a single table.  It does not cover:  Joins and other table to table interactions. Bob Litsinger  Using sub-queries in a primary query. bob.litsinger@gmail.com  The use of the many functions available in SQL.  There are a few examples of functions, and an additional topics list at the end.
  • 3. Contents SQL Select . . . the very basics Introduction 2 More on ORDER BY 13 Contents 3 Having 14 English like 4 Operators 15 Simple Select 5 Qualifiers for WHERE 16 Specifying the fields 6 More on qualifiers for WHERE 17 Introducing WHERE clauses 7 BETWEEN with datetime type 18 Bob Litsinger bob.litsinger@gmail.com Exploring information 8 Wildcards with LIKE 19 Count – How Many Products 9 ALIASES, CAST and CONVERT 20 A few aggregations 10 Other Capabilities 21 Establishing order 11 Queries Unleashed 22 More on GROUP BY 12
  • 4. English like  Basic SQL is very intuitive for users because a request is English like: Bob Litsinger bob.litsinger@gmail.com  . . . but using this structure, we can ask complicated questions.
  • 5. Simple Select  I wonder what types of products Adventure * Will show us all the details (called fields) about the Categories Bob Litsinger bob.litsinger@gmail.com  To much information? You can get just the ID and Name by being more specific.
  • 6. Specify the fields  So, let’s get the Category ID, Name and Manager: Bob Litsinger bob.litsinger@gmail.com  Using the Category ID for Bikes (1), the types of bikes can be found in the Subcategory table.
  • 7. Introducing WHERE clauses  To limit the Sub Categories to bikes, a WHERE clause is added: Bob Litsinger bob.litsinger@gmail.com
  • 8. Exploring information  Looking only at Road Bikes in the Product line up will enable us to explore some data in other ways. Bob Litsinger bob.litsinger@gmail.com  Gives us a list of 43 bikes More are listed
  • 9. Count - How Many Products  The count(*) function provides a simple count of the bikes This assigns a name to a column. If the name has spaces, keywords or some other characters it requires brackets. Bob Litsinger All Products bob.litsinger@gmail.com The word AS is optional, but makes things clearer. If there are no spaces or invalid characters, brackets are not needed. Just Road Bikes Introducing column labels
  • 10. A few aggregations. Aggregations allow I can add a label to each row. It is listed as a query item and is in quotes. analysis of numeric values. Simple count of number of Road 52 cm road bikes by color Finds the minimum, i.e. lowest, value Calculates the average value. Finds the maximum, i.e. Bob Litsinger highest, value bob.litsinger@gmail.com To calculate the Notice the WHERE clause can aggregates the include many conditions query needs to know how the listing is grouped. A variation: Finds the distinct number of colors of road bikes (for all sizes).
  • 11. Establishing order SUM() is the most frequently This is a function that returns the number of the month. Functions will be discussed briefly later. used aggregate. Notice something else new. An ORDER BY clause was added to put the months in Bob Litsinger bob.litsinger@gmail.com order by occurrence.
  • 12. More on GROUP BY If the GROUP BY clause is left off If the GROUP BY clause is incomplete Bob Litsinger bob.litsinger@gmail.com Color is not listed! However, if the aggregate applies only to the entire query with no breakdown by any category, then a GROUP BY is not needed.
  • 13. More on ORDER BY  It is always the last cause. Sorting is the last step for the SQL engine, and the request for it is placed last.  Can be against multiple fields, and any field order. Bob Litsinger bob.litsinger@gmail.com  Does not need to agree with the fields or field order in GROUP BY  Is the only place in a query script where a field alias can be used.
  • 14. HAVING Works like a “WHERE” clause but for aggregations Bob Litsinger bob.litsinger@gmail.com
  • 15. Operators + Addition and Concatenation Used in WHERE clause . . . - Subtraction * Multiplication / Division = Equals <> Does Not Equal != Does Not Equal Bob Litsinger bob.litsinger@gmail.com >= Greater Than or Equal To <= Less Than or Equal To > Greater Than < Less Than % Mod operator (also used as wildcard) . . . or to calculate a value.
  • 16. Qualifiers for WHERE There are some special operators that can be used in the WHERE clause. These can be used with NOT. Bob Litsinger bob.litsinger@gmail.com For LIKE there are even some special wildcard characters.
  • 17. More on qualifiers for WHERE IN and NOT IN Bob Litsinger bob.litsinger@gmail.com Be sure your range is “inclusive” when you use between. Using a single beginning letter will not get the Subcategories that begin with “M.” To get all these add zzz: With datetime data type
  • 18. BETWEEN with datetime type The datetime data type has a time with it. If the data actually records time between will leave out records if you rely on implicit text to datetime conversion. This small test tables shows the problem. Bob Litsinger bob.litsinger@gmail.com These two queries solve the problem: Both get all the records.
  • 19. Wildcards with LIKE Bob Litsinger bob.litsinger@gmail.com
  • 20. Aliases, Convert and Cast  Some ways to make things more readable are shown in the slide  Many of the slides use field aliases to provide user friendly column labels.  CAST and CONVERT are both used in places. These make query results more presentable. Bob Litsinger bob.litsinger@gmail.com  There are other options (like STR) and CONVERT/CAST setting that you can use. There are specific settings in CONVERT for date formats.
  • 21. Other Capabilities  Advanced aggregations like CUBE, ROLLUP and PIVOT  Specific Math, String, Date, Text, Image and other Functions Bob Litsinger  Program controls like IF, WHILE, CASE, etc. bob.litsinger@gmail.com  Stored Procedures and Functions  System Functions and Procedures  Triggers and Database Management
  • 22. Queries Unleashed To really tapped the power of a relational database by using multiple tables.  Use tables together with JOIN, UNION, EXCEPT Bob Litsinger and MERGE. bob.litsinger@gmail.com  Use sub-queries to help the extend the power of a primary query. Check out other existing and future slide shows for examples of other SQL functions, capabilities and scripting.