Writing Basic SQL SELECT Statements
sql SQL stands for Structured Query Language SQL lets you access and manipulate databases
Types of SQL Statements
Writing SQL Statements SQL statements are not case sensitive. SQL statements can be on one or more lines. Keywords cannot be abbreviated or split across lines. Clauses are usually placed on separate lines.
Basic SELECT Statement Select { *, column_name} from Table_name; SELECT identifies what columns FROM idenifies which table
In the syntax: KEYWORD MEANING SELECT is a list of one or more columns * selects all columns Column-expression selects the named column or the expression FROM table specifies the table containing the columns
Selecting All Columns
Selecting Specific Columns
Arithmetic Expressions Create expressions with number and date data by using arithmetic operators.
Using Arithmetic Operators Note: Resultant calculated column vacationHours+10 is not a new column in the humanResources.Employee table; it is for display only. By default, there is no name for the new column
Operator Precedence Multiplication and division take priority over addition and subtraction Operators of the same priority are evaluated from left to right Parentheses are used to force prioritized evaluation and to clarify statements
Operator Precedence
Using Parentheses
Defining a Column Alias Renames a column heading Is useful with calculations Immediately follows the column name: there can also be the optional AS keyword between the column name and alias Requires double quotation marks if it contains spaces or special characters or is case sensitive
Using Column Aliases
Concatenation Operator A concatenation operator: Concatenates columns or character strings to other columns It uses the function of CONCAT(); It requires a minimum of two input values; otherwise, an error is raised.
Using the Concatenation Operator
Literal Character Strings A literal value is a character, a number, or a date included in the SELECT list Date and character literal values must be enclosed within single quotation marks Each character string is output once for each row returned
Using Literal Character Strings
Using Literal Character Strings
Duplicate Rows The default display of queries is all rows, including duplicate rows.
Eliminating Duplicate Rows Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
Using Where Clause
Restrict the rows returned by using the WHERE clause The WHERE clause follows the FROM clause In this syntax: KEYWORD MEANING WHERE restricts the query to rows that meet a condition condition is composed of column names, a comparison operator and constant |list of values|column names
Character Strings and Dates Character strings and dates in the WHERE clause must be enclosed in single quotation marks (''). Number or constants, however should not be enclosed in single quotation marks.
Comparison Conditions OPERATOR MEANING = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to
Comparison Conditions Comparison conditions are used in conditions that compare one expression to another value or expression. They are used in the WHERE clause in the following format: ... WHERE expr operator value Example ... WHERE hire_date='01-JAN-95' ... WHERE salary>=6000 ... WHERE last_name='Smith‘ An alias cannot be used in the WHERE clause.
Using comparison Condition
Other Comparison Conditions OPERATOR MEANING BETWEEN ...AND... Between two values (inclusive) IN(set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value
Using the BETWEEN Condition Use the BETWEEN condition to display rows based on a range of values. Values specified with the BETWEEN condition are inclusive. You must specify the lower limit first.
Using the IN Condition Use the IN membership condition to test for values in a list.
Using the IN Condition(Contd..) The IN condition can be used with any data type. The following example returns a row from the EMPLOYEES table for any employee whose last name is included in the list of names in the WHERE clause: If characters or dates are used in the list, they must be enclosed in single quotation marks ('').
Using the LIKE Condition Use the LIKE condition to perform character pattern-matching operation Search conditions can contain either literal characters or numbers: – % denotes zero or many characters. – _ denotes one character.
Use like Condition
Using the LIKE Condition You can combine pattern-matching characters.
Using the NULL Conditions Test for nulls with the IS NULL operator.
Logical Conditions A logical condition combines the result of two component conditions to produce a single result based on them or inverts the result of a single condition. A row is returned only if the overall result of the condition is true Three logical operators are available in SQL: • AND • OR • NOT
Logical Conditions(Contd..) OPERATOR MEANING AND Returns TRUE if both component conditions are true OR Returns TRUE if either component condition is true NOT Returns TRUE if the following condition is false You can use several conditions in one WHERE clause using the AND and OR operators.
Using the AND Operator AND requires both conditions to be true.
Using the OR Operator OR requires either condition to be true.
Using Not operator in select
Rules of Precedence
ORDER BY Clause Sort rows with the ORDER BY clause – ASC: ascending order (the default order) – DESC: descending order The ORDER BY clause comes last in the SELECT statement. A 1 Z 9 Z 9 A 1
Order by clause
Sorting in Descending order
Sorting by Column Alias
Sorting by Column Position in SELECT List 1 2 3 4 SELECT employee_id, first_name, last_name, department_id FROM employees ORDER BY 2 ; … …
Sorting by Multiple Columns The order of ORDER BY list is the order of sort.
exercise 1. Create a query to display the job title and vacation hours of the employee whose hours are greater than 50. (hint: humanresources.employee table) 2. Create a query to display the Gender, birthday and job title for employee whose date of birth is 1969-1-29 3. Create a query to display the sales orderID and unit price for all the sales which are not under the range of 1500 and 2850 (hint: sales.salesorderdetail table)
exercise 4. Display the employee job title, Gender and hire date of employees hired between February 20, 2007 and May 1, 2008. order the query in ascending order by hire date. 5. Display the currency code of following countries Pakistan Rupees, saudi Riyal and singapore Dollar. (hint: use sales.currency table) 6. List the job title, hire date and vacation hours of the employee whose vacation hours are greater than 30. lable the column as job description, joining date and unpaid hours respectively (hint: use humanresource.employee table)
Exercise 7. Display the first, middle and last name of the employees whose name starts with ‘D’ (Hint: use person.person table) 8. Display the first name of the employee whose middle name is not null. 9. Display the department id, name and groupname and sort the data in the descending order by groupname. 10. Display the names of all employees where third letter of their name is an A
Exercise 11. Display the names of all the employees that have two Ls in their name and have title of ‘Mr.’ 12. Display the job title, nationalIDnumber, hire date for all the employees whose job title is marketing assistant, accounts manager, recruiter and their vacation hours are not equal to 10,15 and 50. 13. Display the job title, vacation hours, vacation hrs in days in a separate column named vacations in days
The End