Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Basic SQL SELECT Statements

Similar presentations


Presentation on theme: "Writing Basic SQL SELECT Statements"— Presentation transcript:

1 Writing Basic SQL SELECT Statements

2 sql SQL stands for Structured Query Language
SQL lets you access and manipulate databases

3 Types of SQL Statements

4 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.

5 Basic SELECT Statement
Select { *, column_name} from Table_name; SELECT identifies what columns FROM idenifies which table

6 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

7 Selecting All Columns

8 Selecting Specific Columns

9 Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.

10 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

11 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

12 Operator Precedence

13 Using Parentheses

14 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

15 Using Column Aliases

16 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.

17 Using the Concatenation Operator

18 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

19 Using Literal Character Strings

20 Using Literal Character Strings

21 Duplicate Rows The default display of queries is all rows, including duplicate rows.

22 Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.

23 Using Where Clause

24 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

25 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.

26 Comparison Conditions
OPERATOR MEANING = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to

27 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>= WHERE last_name='Smith‘ An alias cannot be used in the WHERE clause.

28 Using comparison Condition

29 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

30 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.

31 Using the IN Condition Use the IN membership condition to test for values in a list.

32 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 ('').

33 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.

34 Use like Condition

35 Using the LIKE Condition
You can combine pattern-matching characters.

36 Using the NULL Conditions
Test for nulls with the IS NULL operator.

37 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

38 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.

39 Using the AND Operator AND requires both conditions to be true.

40 Using the OR Operator OR requires either condition to be true.

41 Using Not operator in select

42 Rules of Precedence

43 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 Z Z A 1

44 Order by clause

45 Sorting in Descending order

46 Sorting by Column Alias

47 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 ;

48 Sorting by Multiple Columns
The order of ORDER BY list is the order of sort.

49 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 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)

50 exercise 4. Display the employee job title, Gender and hire date of employees hired between February 20, 2007 and May 1, 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)

51 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

52 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 Display the job title, vacation hours, vacation hrs in days in a separate column named vacations in days

53 The End


Download ppt "Writing Basic SQL SELECT Statements"

Similar presentations


Ads by Google