SQL Select. . . the very basics Bob Litsinger email@example.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 firstname.lastname@example.org 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 18Bob Litsingerbob.email@example.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 Litsingerbob.firstname.lastname@example.org . . . 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 CategoriesBob Litsingerbob.email@example.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 Litsingerbob.firstname.lastname@example.org 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 Litsingerbob.email@example.com
Exploring information Looking only at Road Bikes in the Product line up will enable us to explore some data in other ways.Bob Litsingerbob.firstname.lastname@example.org 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 Productsbob.email@example.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, firstname.lastname@example.org 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 inBob Litsingerbob.email@example.com order by occurrence.
More on GROUP BY If the GROUP BY clause is left off If the GROUP BY clause is incompleteBob Litsingerbob.firstname.lastname@example.org 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 Litsingerbob.email@example.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 aggregationsBob Litsingerbob.firstname.lastname@example.org
Operators + Addition and Concatenation Used in WHERE clause . . . - Subtraction * Multiplication / Division = Equals <> Does Not Equal != Does Not EqualBob Litsingerbob.email@example.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 Litsingerbob.firstname.lastname@example.org For LIKE there are even some special wildcard characters.
More on qualifiers for WHERE IN and NOT INBob Litsingerbob.email@example.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 Litsingerbob.firstname.lastname@example.org These two queries solve the problem: Both get all the records.
Wildcards with LIKEBob Litsingerbob.email@example.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 Litsingerbob.firstname.lastname@example.org 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 FunctionsBob Litsinger Program controls like IF, WHILE, CASE, email@example.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, EXCEPTBob Litsinger and MERGE.firstname.lastname@example.org 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.