Sql Basic Selects

431 views

Published on

Using single tables only, this slide show reviews and explains basic SQL syntax in T-SQL.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
431
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql Basic Selects

  1. 1. SQL Select. . . the very basics Bob Litsinger bob.litsinger@gmail.com
  2. 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. 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 18Bob Litsingerbob.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. 4. English like  Basic SQL is very intuitive for users because a request is English like:Bob Litsingerbob.litsinger@gmail.com  . . . but using this structure, we can ask complicated questions.
  5. 5. Simple Select  I wonder what types of products Adventure * Will show us all the details (called fields) about the CategoriesBob Litsingerbob.litsinger@gmail.com  To much information? You can get just the ID and Name by being more specific.
  6. 6. Specify the fields  So, let’s get the Category ID, Name and Manager:Bob Litsingerbob.litsinger@gmail.com  Using the Category ID for Bikes (1), the types of bikes can be found in the Subcategory table.
  7. 7. Introducing WHERE clauses  To limit the Sub Categories to bikes, a WHERE clause is added:Bob Litsingerbob.litsinger@gmail.com
  8. 8. Exploring information  Looking only at Road Bikes in the Product line up will enable us to explore some data in other ways.Bob Litsingerbob.litsinger@gmail.com  Gives us a list of 43 bikes More are listed
  9. 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 Productsbob.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. 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, valuebob.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. 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 inBob Litsingerbob.litsinger@gmail.com order by occurrence.
  12. 12. More on GROUP BY If the GROUP BY clause is left off If the GROUP BY clause is incompleteBob Litsingerbob.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. 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 Litsingerbob.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. 14. HAVING Works like a “WHERE” clause but for aggregationsBob Litsingerbob.litsinger@gmail.com
  15. 15. Operators + Addition and Concatenation Used in WHERE clause . . . - Subtraction * Multiplication / Division = Equals <> Does Not Equal != Does Not EqualBob Litsingerbob.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. 16. Qualifiers for WHERE There are some special operators that can be used in the WHERE clause. These can be used with NOT.Bob Litsingerbob.litsinger@gmail.com For LIKE there are even some special wildcard characters.
  17. 17. More on qualifiers for WHERE IN and NOT INBob Litsingerbob.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. 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 Litsingerbob.litsinger@gmail.com These two queries solve the problem: Both get all the records.
  19. 19. Wildcards with LIKEBob Litsingerbob.litsinger@gmail.com
  20. 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 Litsingerbob.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. 21. Other Capabilities  Advanced aggregations like CUBE, ROLLUP and PIVOT  Specific Math, String, Date, Text, Image and other FunctionsBob 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. 22. Queries Unleashed To really tapped the power of a relational database by using multiple tables.  Use tables together with JOIN, UNION, EXCEPTBob 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.

×