forked from ahmedsami76/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84555ce
commit b8a944c
Showing
1 changed file
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
-- Active: 1688066368502@@pg-db@5432@sfpolice | ||
-- Active: 1688066368502@@pg-db@5432@hr | ||
-- get the data from the below link | ||
-- https://data.sfgov.org/Public-Safety/Police-Department-Incident-Reports-Historical-2003/tmnf-yvry/data | ||
|
||
|
@@ -9,7 +9,7 @@ | |
SELECT COUNT(DISTINCT pddistrict) FROM police_incident_reports; | ||
|
||
-- How many neighborhoods are there? | ||
SELECT COUNT(DISTINCT "Neighborhoods 2") FROM police_incident_reports; | ||
SELECT COUNT(DISTINCT 'Neighborhoods 2") FROM police_incident_reports; | ||
-- How many incidents by neighborhood? | ||
SELECT "Neighborhoods 2", COUNT(*) AS COUNT FROM police_incident_reports GROUP BY "Neighborhoods 2" order by COUNT(*); -- Try to visualize in pgadmin | ||
|
@@ -92,4 +92,154 @@ select *, count(incidentnum) over() from police_incident_reports | |
Select category, pddistrict, descript, count(incidentnum) over() from police_incident_reports | ||
|
||
-- change the scope of the window to the category alone | ||
Select category, pddistrict, descript, count(incidentnum) over(partition by category) from police_incident_reports | ||
Select category, pddistrict, descript, count(incidentnum) over(partition by category) from police_incident_reports | ||
|
||
-- if you would like to get the equivalent to Group By | ||
Select category, count(incidentnum) over(partition by category) from police_incident_reports -- this will list all rows with the count without flattening the rows | ||
-- You must add DISTINCT statement to the above to get the equivalent to Group By | ||
Select DISTINCT category, count(incidentnum) over(partition by category) from police_incident_reports | ||
|
||
|
||
-- row_number() | ||
-- return a unique number for each row | ||
select *, row_number() over() from police_incident_reports | ||
|
||
-- you can create different windows by spcifying a partition | ||
select category, pddistrict, date, row_number() over(partition by category) from police_incident_reports | ||
|
||
-- order the window to change the row number | ||
select category, pddistrict, date, row_number() over(partition by category order by date) from police_incident_reports | ||
|
||
-- show the first 2 incidents by category and pddistrict | ||
-- use subquery | ||
select * from ( | ||
select category, pddistrict, date, (row_number() over(partition by category order by date)) as rn from police_incident_reports | ||
) x | ||
where x.rn < 3 | ||
|
||
-- rank() | ||
|
||
select category, pddistrict, rank() over(partition by "Incident Code" order by pddistrict) from police_incident_reports | ||
|
||
-- dense_rank() | ||
select category, pddistrict, dense_rank() over(partition by "Incident Code" order by pddistrict) from police_incident_reports | ||
|
||
|
||
|
||
-- as another example create a new database and table | ||
create database hr; | ||
|
||
-- create and populate employee table | ||
CREATE TABLE employees ( | ||
employee_id INT, | ||
first_name VARCHAR (20) DEFAULT NULL, | ||
last_name VARCHAR (25) NOT NULL, | ||
email VARCHAR (100) NOT NULL, | ||
phone_number VARCHAR (20) DEFAULT NULL, | ||
hire_date DATE NOT NULL, | ||
job_id INT NOT NULL, | ||
salary DECIMAL (8, 2) NOT NULL, | ||
manager_id INT DEFAULT NULL, | ||
department_id VARCHAR (25) DEFAULT NULL | ||
); | ||
|
||
|
||
|
||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (100,'Steven','King','[email protected]','515.123.4567','1987-06-17',4,24000.00,NULL,'Executive'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (101,'Neena','Kochhar','[email protected]','515.123.4568','1989-09-21',5,17000.00,100,'Executive'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (102,'Lex','De Haan','lex.de [email protected]','515.123.4569','1993-01-13',5,17000.00,100,'Executive'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (103,'Alexander','Hunold','[email protected]','590.423.4567','1990-01-03',9,9000.00,102,'IT'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (104,'Bruce','Ernst','[email protected]','590.423.4568','1991-05-21',9,6000.00,103,'IT'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (105,'David','Austin','[email protected]','590.423.4569','1997-06-25',9,4800.00,103,'IT'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (106,'Valli','Pataballa','[email protected]','590.423.4560','1998-02-05',9,4800.00,103,'IT'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (107,'Diana','Lorentz','[email protected]','590.423.5567','1999-02-07',9,4200.00,103,'IT'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (108,'Nancy','Greenberg','[email protected]','515.124.4569','1994-08-17',7,12000.00,101,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (109,'Daniel','Faviet','[email protected]','515.124.4169','1994-08-16',6,9000.00,108,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (110,'John','Chen','[email protected]','515.124.4269','1997-09-28',6,8200.00,108,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (111,'Ismael','Sciarra','[email protected]','515.124.4369','1997-09-30',6,7700.00,108,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (112,'Jose Manuel','Urman','jose [email protected]','515.124.4469','1998-03-07',6,7800.00,108,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (113,'Luis','Popp','[email protected]','515.124.4567','1999-12-07',6,6900.00,108,'Finance'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (114,'Den','Raphaely','[email protected]','515.127.4561','1994-12-07',14,11000.00,100,'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (115,'Alexander','Khoo','[email protected]','515.127.4562','1995-05-18',13,3100.00,114, 'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (116,'Shelli','Baida','[email protected]','515.127.4563','1997-12-24',13,2900.00,114,'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (117,'Sigal','Tobias','[email protected]','515.127.4564','1997-07-24',13,2800.00,114,'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (118,'Guy','Himuro','[email protected]','515.127.4565','1998-11-15',13,2600.00,114,'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (119,'Karen','Colmenares','[email protected]','515.127.4566','1999-08-10',13,2500.00,114,'Administration Assistant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (120,'Matthew','Weiss','[email protected]','650.123.1234','1996-07-18',19,8000.00,100,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (121,'Adam','Fripp','[email protected]','650.123.2234','1997-04-10',19,8200.00,100,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (122,'Payam','Kaufling','[email protected]','650.123.3234','1995-05-01',19,7900.00,100,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (123,'Shanta','Vollman','[email protected]','650.123.4234','1997-10-10',19,6500.00,100,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (126,'Irene','Mikkilineni','[email protected]','650.124.1224','1998-09-28',18,2700.00,120,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (145,'John','Russell','[email protected]',NULL,'1996-10-01',15,14000.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (146,'Karen','Partners','[email protected]',NULL,'1997-01-05',15,13500.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (176,'Jonathon','Taylor','[email protected]',NULL,'1998-03-24',16,8600.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (177,'Jack','Livingston','[email protected]',NULL,'1998-04-23',16,8400.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (178,'Kimberely','Grant','[email protected]',NULL,'1999-05-24',16,7000.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (179,'Charles','Johnson','[email protected]',NULL,'2000-01-04',16,6200.00,100,'Sales'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (192,'Sarah','Bell','[email protected]','650.501.1876','1996-02-04',17,4000.00,123,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (193,'Britney','Everett','[email protected]','650.501.2876','1997-03-03',17,3900.00,123,'Shipping'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (200,'Jennifer','Whalen','[email protected]','515.123.4444','1987-09-17',3,4400.00,101,'Public Accountant'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (201,'Michael','Hartstein','[email protected]','515.123.5555','1996-02-17',10,13000.00,100,'Accounting Manager'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (202,'Pat','Fay','[email protected]','603.123.6666','1997-08-17',11,6000.00,201,'Accounting Manager'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (203,'Susan','Mavris','[email protected]','515.123.7777','1994-06-07',8,6500.00,101,'President'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (204,'Hermann','Baer','[email protected]','515.123.8888','1994-06-07',12,10000.00,101,'Public Relations'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (205,'Shelley','Higgins','[email protected]','515.123.8080','1994-06-07',2,12000.00,101,'Accounting'); | ||
INSERT INTO employees(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES (206,'William','Gietz','[email protected]','515.123.8181','1994-06-07',1,8300.00,205,'Accounting'); | ||
|
||
|
||
------- | ||
-- rank() and dense_rank() | ||
------- | ||
SELECT | ||
first_name, | ||
last_name, | ||
department_id, | ||
salary, | ||
RANK() OVER(partition by department_id ORDER BY salary DESC) rank | ||
FROM | ||
employees; | ||
|
||
SELECT | ||
first_name, | ||
last_name, | ||
department_id, | ||
salary, | ||
RANK() OVER(partition by department_id ORDER BY salary DESC) rank, | ||
DENSE_RANK() OVER(partition by department_id ORDER BY salary DESC) dense_rank | ||
FROM | ||
employees; | ||
|
||
-- lead() and lag() | ||
SELECT | ||
first_name, | ||
last_name, | ||
department_id, | ||
salary, | ||
LAG(salary, 1) OVER(partition by department_id ORDER BY salary DESC) lag, | ||
salary - LAG(salary, 1) OVER(partition by department_id ORDER BY salary DESC) diff | ||
FROM | ||
employees; | ||
|
||
SELECT | ||
first_name, | ||
last_name, | ||
department_id, | ||
salary, | ||
LEAD(salary, 1) OVER(partition by department_id ORDER BY salary DESC) lead, | ||
salary - LEAD(salary, 1) OVER(partition by department_id ORDER BY salary DESC) diff | ||
FROM | ||
employees; | ||
|
||
|
||
-- ntile() | ||
SELECT | ||
first_name, | ||
last_name, | ||
department_id, | ||
salary, | ||
NTILE(4) OVER(partition by department_id ORDER BY salary) quartile | ||
FROM | ||
employees | ||
ORDER BY | ||
department_id, | ||
quartile; |