Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced SQL Aggregations, Grouping, SQL Functions, DDL SoftUni Team Technical Trainers Software University

Similar presentations


Presentation on theme: "Advanced SQL Aggregations, Grouping, SQL Functions, DDL SoftUni Team Technical Trainers Software University"— Presentation transcript:

1 Advanced SQL Aggregations, Grouping, SQL Functions, DDL SoftUni Team Technical Trainers Software University http://softuni.bg

2 Table of Contents 1.Nested SELECT Statements 2.Aggregating Data  Group Functions: COUNT, SUM, MIN, MAX  Grouping with GROUP BY 3.Microsoft SQL Server Functions 4.Data Definition Language (DDL)  Creating Tables in MS SQL Server 5.Transactions in MS SQL Server 2

3 SQL Language Nested SELECT Statements

4 4  SELECT statements can be nested in the WHERE clause  Note: prefer joins to nested SELECT for better performance Nested SELECT Statements SELECT FirstName, LastName, Salary FROM Employees WHERE Salary = (SELECT MAX(Salary) FROM Employees) (SELECT MAX(Salary) FROM Employees) SELECT FirstName, LastName, DepartmentID, Salary FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Name='Sales') (SELECT DepartmentID FROM Departments WHERE Name='Sales') FirstNameLastNameSalary KenSanchez125500.00

5 5  Tables from the outer SELECT can be referred in the inner SELECT by aliases  Example: find the highest salary for each department and the employee that takes it Nested SELECT with Table Aliases SELECT FirstName, LastName, DepartmentID, Salary FROM Employees e WHERE Salary = (SELECT MAX(Salary) FROM Employees (SELECT MAX(Salary) FROM Employees WHERE DepartmentID = e.DepartmentID) WHERE DepartmentID = e.DepartmentID) ORDER BY DepartmentID

6 6  Using the EXISTS operator in SELECT statements  Find all employees with managers from the first department Using the EXISTS Operator SELECT FirstName, LastName, EmployeeID, ManagerID FROM Employees e WHERE EXISTS (SELECT EmployeeID FROM Employees m (SELECT EmployeeID FROM Employees m WHERE m.EmployeeID = e.ManagerID AND m.DepartmentID = 1) WHERE m.EmployeeID = e.ManagerID AND m.DepartmentID = 1) FirstNameLastNameEmployeeIDManagerID RobertoTamburello312 RobWalters43 …………

7 SQL Language Aggregating Data with Group Functions

8 8  Group functions operate over sets of rows to return one single result (per group) Group FunctionsEmployeeIDSalary1 12500.00 2 13500.00 3 43300.00 4 29800.00 5 25000.00...... MAX(Salary) 125500.00

9 9  COUNT(*) – count of the selected rows  COUNT(column) – count of the non-null values in given column  SUM(column) – sum of the values in given column  MIN(column) – the minimal value in given column  MAX(column) – the maximal value in given column  AVG(column) – average of the values in given column Group Functions in SQL

10 10  You can use MIN() and MAX() for almost any data type ( int, datetime, varchar,...)  Show the first and last employee's name in alphabetical order: MIN() and MAX() Functions SELECT MIN(HireDate) MinHD, MAX(HireDate) MaxHD FROM Employees MinHDMaxHD 1996-07-312003-06-03 SELECT MIN(LastName), MAX(LastName) FROM Employees

11 11  You can use AVG() and SUM() only for numeric data types AVG() and SUM() Functions SELECT AVG(Salary) [Average Salary], AVG(Salary) [Average Salary], MAX(Salary) [Max Salary], MAX(Salary) [Max Salary], MIN(Salary) [Min Salary], MIN(Salary) [Min Salary], SUM(Salary) [Salary Sum] SUM(Salary) [Salary Sum] FROM Employees WHERE JobTitle = 'Production Technician' Average Salary Max Salary Min Salary Salary Sum 12267.515915000.009500.001926000.00

12 12  COUNT(*) returns the number of rows in the result record set non-null  COUNT(expr) returns the number of rows with non-null values The COUNT(…) Function SELECT COUNT(*) Cnt FROM Employees WHERE DepartmentID = 3 Cnt 18 SELECT COUNT(ManagerID) MgrCount, COUNT(*) AllCount COUNT(*) AllCount FROM Employees WHERE DepartmentID = 16 MgrCountAllCount12

13 13  Group functions ignore NULL values in the target column  If each NULL value in the ManagerID column were considered as 0 in the calculation, the result would be 106 Group Functions and NULLs SELECT AVG(ManagerID) Avg, AVG(ManagerID) Avg, SUM(ManagerID) / COUNT(*) AvgAll SUM(ManagerID) / COUNT(*) AvgAll FROM Employees AvgAvgAll 108106

14 14  Find the earliest hired employee for each department Group Functions in Nested Queries SELECT e.FirstName, e.LastName, e.HireDate, d.Name as Dept FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID ON e.DepartmentID = d.DepartmentID WHERE e.HireDate = (SELECT MIN(HireDate) FROM Employees (SELECT MIN(HireDate) FROM Employees WHERE DepartmentID = d.DepartmentID) WHERE DepartmentID = d.DepartmentID) FirstNameLastNameHireDateDept GuyGilbert 1998-07-31 00:00:00 Production KevinBrown 1999-02-26 00:00:00 Marketing …………

15 SQL Language Group Functions with GROUP BY

16 72000 108600 185600 16 Aggregating Groups of DataDepartmentIDSalary1210300 1216800 1216800 1210300 1217800 228800 225000 229800 225000 16125500 1660100...... DepartmentID SUM (Salary) 1272000 2108600 16185600......

17 17  The GROUP BY clause: split the table rows into groups  The SELECT + GROUP BY syntax:  The is a list of columns The GROUP BY Statement SELECT, SELECT, FROM FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]

18 18  Example of grouping data:  The GROUP BY column is not required to be in the SELECT list The GROUP BY Statement (2) SELECT DepartmentID, SUM(Salary) as SalariesCost FROM Employees GROUP BY DepartmentID DepartmentIDSalariesCost 1272000 2108600 16185600......

19 19 Grouping by Several Columns 39700 77000 52800 65000 43300DepartmentIDJobTitleSalary11 Network Manager 39700 11 Network Administrator 32500 11 32500 11 Database Administrator 38500 11 38500 10Accountant26400 10Accountant26400 10 Finance Manager 43300......... DepartmentIDJobTitleSalary11 Network Manager 39700 11 Network Administrator 65000 11 Database Administrator 77000 10Accountant52800 10 Finance Manager 43300.........

20 20  Example of grouping data by several columns: Grouping by Several Columns – Example SELECT DepartmentID, JobTitle, SUM(Salary) as Salaries, COUNT(*) as Count SUM(Salary) as Salaries, COUNT(*) as Count FROM Employees GROUP BY DepartmentID, JobTitle DepartmentIDJobTitleSalariesCount 2 Senior Tool Designer 586002 2 Tool Designer 500002 7 Production Supervisor 52500021 7 Production Technician 1926000157............

21 21  This SELECT statement is illegal:  Can not combine columns with group functions unless the columns are in the GROUP BY clause  This SELECT statement is also illegal:  Can not use WHERE for group functions Illegal Use of Group Functions SELECT DepartmentID, COUNT(LastName) FROM Employees SELECT DepartmentID, AVG(Salary) FROM Employees WHERE AVG(Salary) > 30 GROUP BY DepartmentID

22 22  When using groups we can select only columns listed in the GROUP BY and grouping functions over the other columns  Can not select columns not listed in the GROUP BY clause  It is allowed to apply group functions over the columns in the GROUP BY clause, but this has no sense Restrictions for Grouping SELECT DepartmentID, JobTitle, SUM(Salary) AS Cost, MIN(HireDate) as StartDate SUM(Salary) AS Cost, MIN(HireDate) as StartDate FROM Employees GROUP BY DepartmentID, JobTitle

23 23  HAVING works like WHERE but is used for the grouping functions Using GROUP BY with HAVING Clause SELECT DepartmentID, COUNT(EmployeeID) as EmpCount, AVG(Salary) as AverageSalary AVG(Salary) as AverageSalary FROM Employees GROUP BY DepartmentID HAVING COUNT(EmployeeID) BETWEEN 3 AND 5 DepartmentIDEmpCountAverageSalary 2427150 12514400 ………

24 24  Grouping can be applied on columns from joined tables Using Grouping Functions and Table Joins SELECT COUNT(*) AS EmpCount, d.Name AS DeptName FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID ON e.DepartmentID = d.DepartmentID WHERE e.HireDate BETWEEN '1999-2-1' AND '2002-12-31' GROUP BY d.Name HAVING COUNT(*) > 5 ORDER BY EmpCount DESC EmpCountDeptName 95Production 8Finance ……

25 SQL Language SQL Server Functions

26 26  Single-row functions  String functions  Mathematical functions  Date functions  Conversion functions  Multiple-row functions  Aggregate functions Standard Functions in MS SQL Server

27 27  ISNULL(, )  Converts NULL values to given default value ISNULL () Function SELECT Name AS [Projects Name], ISNULL(EndDate, GETDATE()) AS [End Date] ISNULL(EndDate, GETDATE()) AS [End Date] FROM Projects Projects Name End Date Classic Vest 2015-02-11 16:43:00 Cycling Cap 2003-06-01 00:00:00.000 Full-Finger Gloves 2003-06-01 00:00:00.000...... EndDate is NULL, so the current date is returned instead

28 28  Changing the character casing: LOWER(), UPPER()  String manipulation functions: SUBSTRING(), LEN(), LEFT(), RIGHT(), TRIM(), REPLACE() String Functions SELECT LastName, LEN(LastName) AS LastNameLen, UPPER(LastName) AS UpperLastName UPPER(LastName) AS UpperLastName FROM Employees WHERE RIGHT(LastName, 3) = 'son' LastNameLastNameLenUpperLastName Erickson8ERICKSON Johnson7JOHNSON.........

29 29  Mathematical functions: ROUND, FLOOR, POWER, ABS, SQRT, …  Date functions: GETDATE, DATEADD, DAY, MONTH, YEAR, …  Conversion functions: CONVERT, CAST Other Functions SELECT FLOOR(3.14)  3 SELECT ROUND(5.86, 0)  6.00 SELECT CONVERT(DATETIME, '20051231', 112)  2005-12-31 00:00:00.000 -- 112 is the ISO formatting style YYYYMMDD SELECT DATEADD(day, 15, GETDATE())

30 30  We can combine functions to achieve more complex behavior Combining Functions SELECT Name AS [Projects Name], ISNULL(CONVERT(nvarchar(50), EndDate), ISNULL(CONVERT(nvarchar(50), EndDate), 'Not Finished') AS [Date Finished] 'Not Finished') AS [Date Finished] FROM Projects Projects Name Date Finished HL Mountain Front Wheel Jun 1 2003 12:00AM LL Touring Handlebars Not Finished HL Touring Handlebars Not Finished LL Road Front Wheel Jun 1 2003 12:00AM......

31 SQL Language Data Definition Language (DDL)

32 32  Data Definition Language (DDL) for defining / editing objects  CREATE  ALTER  DROP  Data Control Language (DCL) for managing access permissions  GRANT  REVOKE  DENY DDL and DCL Commands

33 33  The SQL CREATE command:  CREATE TABLE ( )  CREATE VIEW AS  CREATE Creating Database Objects CREATE TABLE Persons ( PersonID int IDENTITY, PersonID int IDENTITY, Name nvarchar(100) NOT NULL, Name nvarchar(100) NOT NULL, CONSTRAINT PK_Persons PRIMARY KEY(PersonID)) CONSTRAINT PK_Persons PRIMARY KEY(PersonID))GO CREATE VIEW [First 10 Persons] AS SELECT TOP 10 Name FROM Persons

34 34 Creating Objects – More Examples CREATE TABLE Countries ( CountryID int IDENTITY, CountryID int IDENTITY, Name nvarchar(100) NOT NULL, Name nvarchar(100) NOT NULL, CONSTRAINT PK_Countries PRIMARY KEY(CountryID)) CONSTRAINT PK_Countries PRIMARY KEY(CountryID))GO CREATE TABLE Cities ( CityID int IDENTITY, CityID int IDENTITY, Name nvarchar(100) NOT NULL, Name nvarchar(100) NOT NULL, CountryID int NOT NULL, CountryID int NOT NULL, CONSTRAINT PK_Cities PRIMARY KEY(CityID)) CONSTRAINT PK_Cities PRIMARY KEY(CityID))

35 35  The SQL ALTER command  ALTER TABLE  ALTER Modifying Database Objects -- Add a foreign key constraint Cities --> Country ALTER TABLE Cities ADD CONSTRAINT FK_Cities_Countries FOREIGN KEY (CountryID) REFERENCES Countries(CountryID) FOREIGN KEY (CountryID) REFERENCES Countries(CountryID) -- Add a column Population to the table Country ALTER TABLE Countries ADD Population int -- Remove the column Population from the table Country ALTER TABLE Countries DROP COLUMN Population

36 36  DROP command  DROP TABLE  DROP TRIGGER  DROP INDEX  DROP Deleting Database Objects DROP TABLE Persons ALTER TABLE Cities DROP CONSTRAINT FK_Cities_Countries

37 37  GRANT command  Example:  REVOKE command  Example: Managing Access Permissions GRANT ON TO GRANT ON TO GRANT SELECT ON Persons TO public REVOKE ON FROM REVOKE ON FROM REVOKE SELECT ON Employees FROM public

38 Creating Tables in SQL Server Best Practices

39 39  Creating a new table:  Define the table name  Should have good name  Define the columns and their types  Use proper data type  Define the table primary key  Use IDENTITY for enabling auto increment of the primary key  Define foreign keys and constraints Creating Tables in SQL Server

40 40 CREATE TABLE Groups ( GroupID int IDENTITY, GroupID int IDENTITY, Name nvarchar(100) NOT NULL, Name nvarchar(100) NOT NULL, CONSTRAINT PK_Groups PRIMARY KEY(GroupID)) CONSTRAINT PK_Groups PRIMARY KEY(GroupID)) CREATE TABLE Users ( UserID int IDENTITY, UserID int IDENTITY, UserName nvarchar(100) NOT NULL, UserName nvarchar(100) NOT NULL, GroupID int NOT NULL, GroupID int NOT NULL, CONSTRAINT PK_Users PRIMARY KEY(UserID), CONSTRAINT PK_Users PRIMARY KEY(UserID), CONSTRAINT FK_Users_Groups FOREIGN KEY(GroupID) CONSTRAINT FK_Users_Groups FOREIGN KEY(GroupID) REFERENCES Groups(GroupID)) REFERENCES Groups(GroupID)) Creating Tables in SQL Server – Examples

41 Transactions Begin / Commit / Rollback in SQL Server

42 42  Pessimistic concurrency (default in SQL Server)  Locks table data during each data modification  Concurrent users wait until the lock is released  Optimistic concurrency (default in MySQL and Oracle)  No locks are performed when data is being read or changed  Concurrent users don’t see the changes until they are committed / rolled-back  Supported with the SNAPSHOT isolation level in SQL Server What Is Concurrency Control?

43 43  Transactions start with  BEGIN TRANSACTION (or just BEGIN TRAN )  Use COMMIT to confirm changes and finish the transaction  Use ROLLBACK to cancel changes and abort the transaction  Example: Transactions BEGIN TRAN DELETE FROM EmployeesProjects; DELETE FROM Projects; ROLLBACK TRAN

44 44  What is implicit transactions mode?  Automatically start a new transaction after each commit or rollback  Nested transactions are not allowed  Transaction must be explicitly completed with COMMIT or ROLLBACK TRANSACTION  By default, IMPLICIT_TRANSACITONS setting is switched off The Implicit Transactions Option SET IMPLICIT_TRANSACTIONS ON

45 45 1.How do we nest SELECT statements? 2.How do we aggregate data in SQL?  Explain the group functions in SQL  Explain the GROUP BY clause 3.Explain a few MS SQL Server functions? 4.What are the DDL commands in SQL?  How to create a table in MS SQL Server? 5.How to start / commit / cancel a transaction? Summary

46 ? ? ? ? ? ? ? ? ? https://softuni.bg/courses/databases Advanced SQL

47 License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 47  Attribution: this work may contain portions from  "Databases" course by Telerik Academy under CC-BY-NC-SA licenseDatabasesCC-BY-NC-SA

48 Free Trainings @ Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software University @ Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software University @ YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg


Download ppt "Advanced SQL Aggregations, Grouping, SQL Functions, DDL SoftUni Team Technical Trainers Software University"

Similar presentations


Ads by Google