Presentation is loading. Please wait.

Presentation is loading. Please wait.

Select Statement IT 350. Select Statement SELECT statement is what we use to choose, or select, the data that we want returned from the database to our.

Similar presentations


Presentation on theme: "Select Statement IT 350. Select Statement SELECT statement is what we use to choose, or select, the data that we want returned from the database to our."— Presentation transcript:

1 Select Statement IT 350

2 Select Statement SELECT statement is what we use to choose, or select, the data that we want returned from the database to our application. Common Elements: SELECT FROM WHERE (AND, OR, NOT, BETWEEN, LIKE) ORDER BY

3 SELECT…FROM Clause Basic SELECT – What Column – What tables(s) All columns SELECT * FROM Employees Certain Columns SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees

4 WHERE Clause Limits or filters the data by one (or more) conditions that must be met by the selected data. Achieved by the use of relational operators – =,, <>, = – LIKE, IN, BETWEEN, NOT, NOT IN

5 Employees who live in London SELECT EmployeeID, FirstName, LastName, City FROM Employees WHERE City = 'London' Employees who live anywhere except London SELECT EmployeeID, FirstName, LastName, City FROM Employees WHERE City <> 'London'

6 A list of employees who where hired on or after a given date SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE HireDate >= '1-july-1993'

7 AND Multiple criteria is met Example: Date and City SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE (HireDate >= '1-June-1992') AND (City = 'London') Example: Hired between a certain date SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE (HireDate >= '1-june-1992') AND (HireDate <= '15- december-1993')

8 BETWEEN BETWEEN operator checks to see if a value is between two values (including equality on both ends) SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE HireDate BETWEEN '1-june-1992' AND '15- december-1993'

9 NOT Returns opposite of mentioned criteria Example: NOT BETWEEN SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE HireDate NOT BETWEEN '1-june-1992' AND '15-december-1993'

10 OR Multiple Values SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE City = 'London' OR City = 'Seattle'

11 IN and NOT IN List of multiple values IN Example: SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE City IN ('Seattle', 'Tacoma', 'Redmond') NOT IN Example: SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE City NOT IN ('Seattle', 'Tacoma', 'Redmond')

12 LIKE LIKE operator allows us to perform basic pattern-matching using wildcard characters WildcardDescription _ (underscore)matches any single character %matches a string of one or more characters [ ] matches any single character within the specified range (e.g. [a-f]) or set (e.g. [abcdef]). [^] matches any single character not within the specified range (e.g. [^a-f]) or set (e.g. [^abcdef]).

13 LIKE Examples WHERE FirstName LIKE '_im' finds all three-letter first names that end with 'im' (e.g. Jim, Tim). WHERE LastName LIKE '%stein' finds all employees whose last name ends with 'stein' WHERE LastName LIKE '%stein%' finds all employees whose last name includes 'stein' anywhere in the name. WHERE FirstName LIKE '[JT]im' finds three-letter first names that end with 'im' and begin with either 'J' or 'T' (that is, only Jim and Tim) WHERE LastName LIKE 'm[^c]%' finds all last names beginning with 'm' where the following (second) letter is not 'c'.

14 ORDER BY Order in which the rows appear—sorting the data SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees ORDER BY City

15 ASC, DESC SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM Employees ORDER BY Country, City DESC SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM Employees ORDER BY Country ASC, City DESC SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees ORDER BY Country ASC, City DESC

16

17


Download ppt "Select Statement IT 350. Select Statement SELECT statement is what we use to choose, or select, the data that we want returned from the database to our."

Similar presentations


Ads by Google