Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.

Similar presentations


Presentation on theme: "Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and."— Presentation transcript:

1 Session 9 Accessing Data from a Database

2 RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and use. Explain how to use expressions with SELECT statement. Explain the various clauses used with SELECT. State the use of ORDER BY clause.

3 RDBMS and Data Management/ Session 9/3 of 34 SELECT Statement Displays the required information in a table. Retrieves rows and columns from one or more tables. The output of the SELECT statement is another table called Result Set. Joins two tables or retrieves a subset of columns from one or more tables. Syntax: SELECT … FROM Emp_NoEmp_NameEmp_DOBEmp_DOJ 345James24-Sep-196830-May-1990 873Pamela27-Jul-197019-Nov-1993 693Allan10-Sep-197001-Jul-1992 305Geoff12-Feb-197329-Oct-1996 SELECT Emp_No, Emp_Name, Emp_DOB, Emp_DOJ FROM Employee Result Set

4 RDBMS and Data Management/ Session 9/4 of 34 SELECT without FROM SQL Server allows the use of SELECT statement without FROM. Example : SELECT LEFT(‘International’,5) Output

5 RDBMS and Data Management/ Session 9/5 of 34 Displaying All Columns Asterisk (*) is used to retrieve all the columns from the table. Syntax: Example: SELECT * FROM SELECT * FROM Sales.Individual

6 RDBMS and Data Management/ Session 9/6 of 34 A column list can be used to display relevant columns that are chosen by the user. Syntax: Displaying Selected Columns SELECT.. FROM Example: SELECT LocationID,CostRate FROM Production.Location

7 RDBMS and Data Management/ Session 9/7 of 34 Different Expressions with SELECT Expressions allow the user to view the result set in an ordered manner. They can be used to assign different names to the columns, compute values and eliminate duplicate values in the result set.

8 RDBMS and Data Management/ Session 9/8 of 34 Using Constants in a Result Set Character String Constants are used to join character columns and help in proper formatting. Example: SELECT [Name] +’ : ‘+ CountryRegionCode + ‘ --> ’ +[Group] FROM Sales.SalesTerritory

9 RDBMS and Data Management/ Session 9/9 of 34 Renaming Result Set Column Names Headings can be changed, renamed or can be assigned a new name by using AS clause. Example: SELECT ModifiedDate as ‘ChangedDate’ FROM Sales.Individual

10 RDBMS and Data Management/ Session 9/10 of 34 Computing Values in Result Set Mathematical expressions allow result set to contain values calculated from the values stored in the base table. Example: SELECT ProductID,StandardCost,StandardCost * 0.15 as Discount FROM Production.ProductCostHistory

11 RDBMS and Data Management/ Session 9/11 of 34 Using DISTINCT DISTINCT clause: Prevents the retrieval of duplicate records. Eliminates the rows that are repeating in a result set. Example: SELECT DISTINCT StandardCost FROM Production.ProductCostHistory

12 RDBMS and Data Management/ Session 9/12 of 34 Using TOP and PERCENT TOP keyword will display only the first few set of rows as a result set. The number of rows is limited to a number or a percent of rows. Syntax: SELECT [ALL | DISTINCT] [TOP expression [PERCENT] [WITH TIES]]

13 RDBMS and Data Management/ Session 9/13 of 34 SELECT with INTO Creates a new table Inserts rows and columns listed in the SELECT statement into the new table. The user should have the required permission to CREATE TABLE in the destination database. Syntax : SELECT.. [INTO new_table] FROM table_list Example : SELECT ProductModelID,[Name] INTO Production.ProductName FROM Production.ProductModel  Output

14 RDBMS and Data Management/ Session 9/14 of 34 SELECT with WHERE 1-6 Selects or limits the records retrieved by the query statement. Syntax: SELECT... FROM WHERE Operators: OperatorDescription =Equal to <>, !=Not equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to !Not BETWEENBetween a range LIKESearch for an ordered pattern INWithin a range

15 RDBMS and Data Management/ Session 9/15 of 34 SELECT with WHERE 2-6 Example: SELECT * FROM Production.ProductCostHistory WHERE EndDate = ‘6/30/2003 12:00:00 AM’  Output

16 RDBMS and Data Management/ Session 9/16 of 34 SELECT with WHERE 3-6  Output

17 RDBMS and Data Management/ Session 9/17 of 34 SELECT with WHERE 4-6 Numeric values are not enclosed within any quotes Example: Wild card characters are used with LIKE keyword to make the query accurate and specific. SELECT * FROM HumanResources.Department WHERE DepartmentID < 10 WildcardDescriptionExample _ It will display a single character.SELECT * FROM Person.Contact WHERE Suffix LIKE ‘Jr_’ % It will display a string of any length.SELECT * FROM Person.ContactWHERE LastName LIKE ‘B%’ [ ] It will display a single character Within the range enclosed in the Brackets. SELECT * FROM Sales.CurrencyRate WHERE ToCurrencyCode LIKE ‘C[AN][DY]’ [^] It will display any single character not within the range enclosed in the Brackets. SELECT * FROM Sales.CurrencyRate WHERE ToCurrencyCode LIKE ‘A[^R][^S]’ Wild Card characters

18 RDBMS and Data Management/ Session 9/18 of 34 SELECT with WHERE 5-6 WHERE clause also uses logical operators like AND, OR and NOT. AND operator joins two or more conditions Returns all the rows from the tables where both the conditions that are listed are true. Example: SELECT * FROM Sales.CustomerAddress WHERE AddressID > 900 AND AddressTypeID = 5

19 RDBMS and Data Management/ Session 9/19 of 34 SELECT with WHERE 6-6 OR displays all the rows if it satisfies any one of the conditions. Example: SELECT * FROM Sales.CustomerAddress WHERE AddressID < 900 OR AddressTypeID = 5 NOT operator negates the search condition. Example: SELECT * FROM Sales.CustomerAddress WHERE NOT AddressTypeID = 5

20 RDBMS and Data Management/ Session 9/20 of 34 GROUP BY Clause 1-2 The GROUP BY keyword partitions the result set into one or more subsets. The GROUP BY keyword is followed by a list of columns, known as grouped columns. For every grouped column, there is only one row. It can, however, have more than one grouped column.

21 RDBMS and Data Management/ Session 9/21 of 34 GROUP BY Clause 2-2 Syntax: Example: SELECT.. FROM GROUP BY SELECT WorkOrderID,SUM(ActualResourceHrs) FROM Production.WorkOrderRouting GROUP BY WorkOrderID  Output

22 RDBMS and Data Management/ Session 9/22 of 34 GROUP BY with WHERE Restricts the rows for grouping. Groups the rows that satisfy the search condition. Example SELECT [Group],SUM(SalesYTD) AS ‘TotalSales’ FROM Sales.SalesTerritory WHERE [Group] LIKE ‘N%’ OR [Group] LIKE ‘E%’ GROUP BY [Group]  Output

23 RDBMS and Data Management/ Session 9/23 of 34 GROUP BY with NULL A separate row is formed, if the grouping column contains null values. If the grouping column contains more than one NULL value, the NULL values are put into a single row. Example: SELECT Class, AVG (ListPrice) AS ‘AverageListPrice’ FROM Production.Product GROUP BY Class

24 RDBMS and Data Management/ Session 9/24 of 34 GROUP BY with ALL GROUP BY with ALL includes all the groups that the GROUP BY clause produces. SELECT FROM WHERE GROUP BY ALL SELECT [Group],SUM(SalesYTD) AS ‘TotalSales’ FROM Sales.SalesTerritory WHERE [Group] LIKE ‘N%’ OR [Group] LIKE ‘E%’ GROUP BY ALL [Group]  Output Syntax: Example:

25 RDBMS and Data Management/ Session 9/25 of 34 GROUP BY with HAVING It specifies a search condition for a group. Syntax: Example: SELECT FROM GROUP BY HAVING SELECT [Group],SUM(SalesYTD) AS ‘TotalSales’ FROM Sales.SalesTerritory GROUP BY [Group] HAVING SUM(SalesYTD) < 6000000  Output

26 RDBMS and Data Management/ Session 9/26 of 34 Summarizing Data 1-5 Data can be summarized using operators like CUBE and ROLLUP. The number of columns in the GROUP BY clause determines the number of summary rows in the result set.

27 RDBMS and Data Management/ Session 9/27 of 34 Summarizing Data 2-5 CUBE: It produces a super-aggregate row. It provides the summary of the rows that the GROUP BY clause generates. It displays NULL in the result set and returns all the values for those.

28 RDBMS and Data Management/ Session 9/28 of 34 Summarizing Data 3-5 Cube: Syntax: Example: SELECT FROM GROUP BY WITH CUBE SELECT [Name],CountryRegionCode,SUM(SalesYTD) AS TotalSales FROM Sales.SalesTerritory WHERE [Name] <> ‘Australia’ AND [Name]<> ‘Canada’ GROUP BY [Name], CountryRegionCode WITH CUBE  Output

29 RDBMS and Data Management/ Session 9/29 of 34 Summarizing Data 4-5 ROLLUP: It generates a result set that shows groups arranged in a hierarchical order. It arranges the groups from the lowest to the highest. Group hierarchy is dependent on the order in which the columns that are grouped are specified.

30 RDBMS and Data Management/ Session 9/30 of 34 Summarizing Data 5-5 ROLLUP: Syntax: Example: SELECT FROM GROUP BY WITH ROLLUP SELECT [Name],SUM(SalesYTD) AS TotalSales FROM Sales.SalesTerritory GROUP BY [Name]WITH ROLLUP  Output

31 RDBMS and Data Management/ Session 9/31 of 34 ORDER BY Clause ORDER BY: Specifies the order in which the columns should be sorted in a result set. Sorts the result set in ascending (ASC) or descending (DESC) order. In case of multiple fields, the left most field is considered as the primary level of sort.

32 RDBMS and Data Management/ Session 9/32 of 34 ORDER BY Clause Syntax: Example: SELECT FROM ORDER BY column_name> {ASC|DESC} SELECT * FROM Sales.SalesTerritory ORDER BY SalesLastYear  Output

33 RDBMS and Data Management/ Session 9/33 of 34 Summary The output of the SELECT statement is called a result set. The SELECT statement defines the columns to be used in a query. Asterisk (*) is used to display all the columns in the table. AS clause helps in renaming a column in the result set. A Boolean condition is used by the WHERE clause to test the condition of the rows. Operators like CUBE and ROLLUP are used with GROUP BY clause to summarize the data. ORDER BY clause specifies the order in which the columns should be sorted in a result set.


Download ppt "Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and."

Similar presentations


Ads by Google