├── Interview Questions and Answers for Data Analysts.docx ├── README.md ├── Resume Scripts.sql ├── SQL Basics Create Table and Insert Into ├── SQL Unions Tutorial ├── Stored Procedures.sql ├── String Functions ├── Subquery Tutorial.sql └── Temp_Tables /Interview Questions and Answers for Data Analysts.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexTheAnalyst/SQL-Code/36bd27c727b1424bfc5f625391dda567154d6c01/Interview Questions and Answers for Data Analysts.docx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-Code 2 | 3 | I will store all code or documents related to SQL in this repository. 4 | 5 | Please feel free to download and use all code as your own. 6 | -------------------------------------------------------------------------------- /Resume Scripts.sql: -------------------------------------------------------------------------------- 1 | 2 | 3 | TRUNCATE TABLE [Database1].dbo.Example 4 | 5 | 6 | 7 | 8 | INSERT INTO Scorecard.[dbo].Example 9 | SELECT DISTINCT Masterkey 10 | FROM [AggregateDatabase1].dbo.[Example_Table] 11 | WHERE IDOrganization = 279 12 | AND Masterkey LIKE '292%' 13 | 14 | 15 | 16 | 17 | 18 | -- Getting patients first, last, and DOB 19 | 20 | 21 | UPDATE Example 22 | SET [Patient's First Name] = Pers..PsnFirst, [Patient's Late Name] = Pers.PsnLaSt, [Patient's Date of Birth] = Pers.Psnous, [Client Name] = Pers.PcPPracticeName 23 | FROM [AggregateDatabase1].dbo.[Example_Table] Pers 24 | JOIN Scorecard.Example samp 25 | ON Pers.MasterKey = samp.MRN 26 | WHERE Pers.IDOrganization = Pers.IDMasterOrganization 27 | AND Pers.Idstatus = 1 28 | 29 | 30 | 31 | 32 | -- getting most recent phone number 33 | 34 | UPDATE Example 35 | SET [Patient's Phone Number] = replace(replace(replace(replace(replace(replace(replace(replace(LTRIM(RTRIM(A.Phone)),'-',''),'(',''),')',''), ' ' ,''), '/' ,''),'x',''),'*',''),'calls','') 36 | FROM ( 37 | 38 | SELECT Phone [MasterKey],Phone.Phone,Phone.DateUpdated, ROW_NUMBER() OVER (PARTITION BY Phone.MasterKey ORDER BY Phone.DateUpdated DESC) RN 39 | FROM [AggregateDatabase1].dbo.[Phone_Table] Phone 40 | INNER JOIN Scorecard.Example samp 41 | ON Phone.Masterkey = samp.MRN 42 | WHERE PhoneType in ('CELL','HOME') 43 | and ISNULL ([Phone],'') <> '' 44 | and phone not like '%[a-z]%' 45 | and replace(replace(replace(replace(replace(replace(replace(replace(LTRIM(RTRIM(Phone.Phone)),'-',''),'(',''),')',''), ' ' ,''), '/' ,''),'x',''),'*',''),'calls','') NOT LIKE 'e%' 46 | and lenreplace(replace(replace(replace(replace(replace(replace(replace(LTRIM(RTRIM(Phone.Phone)),'-',''),'(',''),')',''), ' ' ,''), '/' ,''),'x',''),'*',''),'calls','') = 10 47 | ) A 48 | 49 | join [AggregateDatabase1].dbo.[Person_Table] j on a.MasterKey = j.MasterKey 50 | join [AggregateDatabase1].dbo.[Master_Patient_Index_Table] d on j.IDOrganization = d.IDOrganization END J.IDPerson = d.IDPerson 51 | INNER JOIN Scorecard.dbo.example Outp 52 | ON A.MasterKey = OutP.MRN 53 | WHERE RN = 1 54 | 55 | 56 | 57 | 58 | 59 | -- formats phone number 60 | 61 | UPDATE Example 62 | SET [Patient's Phone Number] = SUBSTRING([Patient's Phone Number], 1, 3) + '-' + 63 | SUBSTRING{[Patient's Phone Number], 4, 3) + '-' + 64 | SUBSTRING([Patient's Phone Number], 7, 4) 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | SELECT r.ProtCode 81 | ,SUM(CASE WHEN R.Recommendation LIKE '%current' THEN 1 82 | ELSE 0 83 | END) [Met] --Numerator 84 | ,SUM(CASE WHEN R.Recommendation LIKE '%invalid' THEN 1 85 | ELSE 0 86 | END) [Not Met] 87 | ,SUM(CASE WHEN R.Recommendation LIKE '%incl' THEN 1 88 | ELSE 0 89 | END) [Denominator] 90 | ,SUM(CASE WHEN R.Recommendation LIKE '%excl' THEN 1 91 | ELSE 0 92 | END) [Exclusion] 93 | ,SUM(CASE WHEN R.Recommendation LIKE '%exception' THEN 1 94 | ELSE 0 95 | END) [Exception] 96 | , 97 | CONVERT(Decimal(20,1), 98 | (CONVERT{Decimal(20,1), SUM(CASE WHEN R.Recommendation LIKE '%current' THEN 1 ELSE 0 END)*100) ) 99 | / 100 | (SUM(CASE WHEN (R.Recommendation LIKE '%incl' THEN 1 ELSE 0 END)-SUM(CASE WHEN R.Recommendation LIKE '%exception' THEN 1 ELSE 0 END) ) 101 | ) [Performance Rate %] 102 | 103 | 104 | FROM [Example],[dbo].(Recommendations) r WITH(NOLOCK) 105 | 106 | WHERE (R.Recommendation LIKE '%current' 107 | OR R.Recommendation LIKE '%Excl' 108 | OR R.Recommendation LIKE '%Incl' 109 | OR R.Recommendation LIKE '%Invalid' 110 | OR R.Recommendation LIKE '%Exception') 111 | GROUP BY r.ProtCode 112 | ORDER BY 1 113 | -------------------------------------------------------------------------------- /SQL Basics Create Table and Insert Into: -------------------------------------------------------------------------------- 1 | 2 | Table 1 Query: 3 | Create Table EmployeeDemographics 4 | (EmployeeID int, 5 | FirstName varchar(50), 6 | LastName varchar(50), 7 | Age int, 8 | Gender varchar(50) 9 | ) 10 | 11 | Table 2 Query: 12 | Create Table EmployeeSalary 13 | (EmployeeID int, 14 | JobTitle varchar(50), 15 | Salary int 16 | ) 17 | 18 | 19 | 20 | Table 1 Insert: 21 | Insert into EmployeeDemographics VALUES 22 | (1001, 'Jim', 'Halpert', 30, 'Male'), 23 | (1002, 'Pam', 'Beasley', 30, 'Female'), 24 | (1003, 'Dwight', 'Schrute', 29, 'Male'), 25 | (1004, 'Angela', 'Martin', 31, 'Female'), 26 | (1005, 'Toby', 'Flenderson', 32, 'Male'), 27 | (1006, 'Michael', 'Scott', 35, 'Male'), 28 | (1007, 'Meredith', 'Palmer', 32, 'Female'), 29 | (1008, 'Stanley', 'Hudson', 38, 'Male'), 30 | (1009, 'Kevin', 'Malone', 31, 'Male') 31 | 32 | Table 2 Insert: 33 | Insert Into EmployeeSalary VALUES 34 | (1001, 'Salesman', 45000), 35 | (1002, 'Receptionist', 36000), 36 | (1003, 'Salesman', 63000), 37 | (1004, 'Accountant', 47000), 38 | (1005, 'HR', 50000), 39 | (1006, 'Regional Manager', 65000), 40 | (1007, 'Supplier Relations', 41000), 41 | (1008, 'Salesman', 48000), 42 | (1009, 'Accountant', 42000) 43 | -------------------------------------------------------------------------------- /SQL Unions Tutorial: -------------------------------------------------------------------------------- 1 | Table 1 Insert: 2 | Insert into EmployeeDemographics VALUES 3 | (1011, 'Ryan', 'Howard', 26, 'Male'), 4 | (NULL, 'Holly', 'Flax', NULL, NULL), 5 | (1013, 'Darryl', 'Philbin', NULL, 'Male') 6 | 7 | Table 3 Query: 8 | Create Table WareHouseEmployeeDemographics 9 | (EmployeeID int, 10 | FirstName varchar(50), 11 | LastName varchar(50), 12 | Age int, 13 | Gender varchar(50) 14 | ) 15 | 16 | Table 3 Insert: 17 | Insert into WareHouseEmployeeDemographics VALUES 18 | (1013, 'Darryl', 'Philbin', NULL, 'Male'), 19 | (1050, 'Roy', 'Anderson', 31, 'Male'), 20 | (1051, 'Hidetoshi', 'Hasagawa', 40, 'Male'), 21 | (1052, 'Val', 'Johnson', 31, 'Female') 22 | -------------------------------------------------------------------------------- /Stored Procedures.sql: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Today's Topic: Stored Procedures 4 | 5 | */ 6 | 7 | 8 | CREATE PROCEDURE Temp_Employee 9 | AS 10 | DROP TABLE IF EXISTS #temp_employee 11 | Create table #temp_employee ( 12 | JobTitle varchar(100), 13 | EmployeesPerJob int , 14 | AvgAge int, 15 | AvgSalary int 16 | ) 17 | 18 | 19 | Insert into #temp_employee 20 | SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary) 21 | FROM SQLTutorial..EmployeeDemographics emp 22 | JOIN SQLTutorial..EmployeeSalary sal 23 | ON emp.EmployeeID = sal.EmployeeID 24 | group by JobTitle 25 | 26 | Select * 27 | From #temp_employee 28 | GO; 29 | 30 | 31 | 32 | 33 | CREATE PROCEDURE Temp_Employee2 34 | @JobTitle nvarchar(100) 35 | AS 36 | DROP TABLE IF EXISTS #temp_employee3 37 | Create table #temp_employee3 ( 38 | JobTitle varchar(100), 39 | EmployeesPerJob int , 40 | AvgAge int, 41 | AvgSalary int 42 | ) 43 | 44 | 45 | Insert into #temp_employee3 46 | SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary) 47 | FROM SQLTutorial..EmployeeDemographics emp 48 | JOIN SQLTutorial..EmployeeSalary sal 49 | ON emp.EmployeeID = sal.EmployeeID 50 | where JobTitle = @JobTitle --- make sure to change this in this script from original above 51 | group by JobTitle 52 | 53 | Select * 54 | From #temp_employee3 55 | GO; 56 | 57 | 58 | exec Temp_Employee2 @jobtitle = 'Salesman' 59 | exec Temp_Employee2 @jobtitle = 'Accountant' -------------------------------------------------------------------------------- /String Functions: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Today's Topic: String Functions - TRIM, LTRIM, RTRIM, Replace, Substring, Upper, Lower 4 | 5 | */ 6 | 7 | --Drop Table EmployeeErrors; 8 | 9 | 10 | CREATE TABLE EmployeeErrors ( 11 | EmployeeID varchar(50) 12 | ,FirstName varchar(50) 13 | ,LastName varchar(50) 14 | ) 15 | 16 | Insert into EmployeeErrors Values 17 | ('1001 ', 'Jimbo', 'Halbert') 18 | ,(' 1002', 'Pamela', 'Beasely') 19 | ,('1005', 'TOby', 'Flenderson - Fired') 20 | 21 | Select * 22 | From EmployeeErrors 23 | 24 | -- Using Trim, LTRIM, RTRIM 25 | 26 | Select EmployeeID, TRIM(employeeID) AS IDTRIM 27 | FROM EmployeeErrors 28 | 29 | Select EmployeeID, RTRIM(employeeID) as IDRTRIM 30 | FROM EmployeeErrors 31 | 32 | Select EmployeeID, LTRIM(employeeID) as IDLTRIM 33 | FROM EmployeeErrors 34 | 35 | 36 | 37 | 38 | 39 | -- Using Replace 40 | 41 | Select LastName, REPLACE(LastName, '- Fired', '') as LastNameFixed 42 | FROM EmployeeErrors 43 | 44 | 45 | -- Using Substring 46 | 47 | Select Substring(err.FirstName,1,3), Substring(dem.FirstName,1,3), Substring(err.LastName,1,3), Substring(dem.LastName,1,3) 48 | FROM EmployeeErrors err 49 | JOIN EmployeeDemographics dem 50 | on Substring(err.FirstName,1,3) = Substring(dem.FirstName,1,3) 51 | and Substring(err.LastName,1,3) = Substring(dem.LastName,1,3) 52 | 53 | 54 | 55 | -- Using UPPER and lower 56 | 57 | Select firstname, LOWER(firstname) 58 | from EmployeeErrors 59 | 60 | Select Firstname, UPPER(FirstName) 61 | from EmployeeErrors 62 | -------------------------------------------------------------------------------- /Subquery Tutorial.sql: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Today's Topic: Subqueries (in the Select, From, and Where Statement) 4 | 5 | */ 6 | 7 | Select EmployeeID, JobTitle, Salary 8 | From EmployeeSalary 9 | 10 | -- Subquery in Select 11 | 12 | Select EmployeeID, Salary, (Select AVG(Salary) From EmployeeSalary) as AllAvgSalary 13 | From EmployeeSalary 14 | 15 | -- How to do it with Partition By 16 | Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary 17 | From EmployeeSalary 18 | 19 | -- Why Group By doesn't work 20 | Select EmployeeID, Salary, AVG(Salary) as AllAvgSalary 21 | From EmployeeSalary 22 | Group By EmployeeID, Salary 23 | order by EmployeeID 24 | 25 | 26 | -- Subquery in From 27 | 28 | Select a.EmployeeID, AllAvgSalary 29 | From 30 | (Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary 31 | From EmployeeSalary) a 32 | Order by a.EmployeeID 33 | 34 | 35 | -- Subquery in Where 36 | 37 | 38 | Select EmployeeID, JobTitle, Salary 39 | From EmployeeSalary 40 | where EmployeeID in ( 41 | Select EmployeeID 42 | From EmployeeDemographics 43 | where Age > 30) -------------------------------------------------------------------------------- /Temp_Tables: -------------------------------------------------------------------------------- 1 | Create table #temp_employee2 ( 2 | EmployeeID int, 3 | JobTitle varchar(100), 4 | Salary int 5 | ) 6 | 7 | Select * From #temp_employee2 8 | 9 | Insert into #temp_employee2 values ( 10 | '1001', 'HR', '45000' 11 | ) 12 | 13 | Insert into #temp_employee2 14 | SELECT * From SQLTutorial..EmployeeSalary 15 | 16 | Select * From #temp_employee2 17 | 18 | 19 | 20 | 21 | DROP TABLE IF EXISTS #temp_employee3 22 | Create table #temp_employee3 ( 23 | JobTitle varchar(100), 24 | EmployeesPerJob int , 25 | AvgAge int, 26 | AvgSalary int 27 | ) 28 | 29 | 30 | Insert into #temp_employee3 31 | SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary) 32 | FROM SQLTutorial..EmployeeDemographics emp 33 | JOIN SQLTutorial..EmployeeSalary sal 34 | ON emp.EmployeeID = sal.EmployeeID 35 | group by JobTitle 36 | 37 | Select * 38 | From #temp_employee3 39 | 40 | SELECT AvgAge * AvgSalary 41 | from #temp_employee3 42 | 43 | 44 | 45 | --------------------------------------------------------------------------------