-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery1.sql
More file actions
153 lines (113 loc) · 3.77 KB
/
SQLQuery1.sql
File metadata and controls
153 lines (113 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
--CREATE TABLE 1 Query
CREATE TABLE EmployeeDemographics
(EmployeeID int,
FirstName varchar(50),
LastName varchar(50),
Age int,
Gender varchar(50)
)
--CREATE TABLE 2 Query
CREATE TABLE EmployeeSalary
(EmployeeID int,
JobTitle varchar(50),
Salary int)
--Insert value in table 1 Query
INSERT INTO EmployeeDemographics VALUES
(1001, 'Jim', 'Halpert', 30, 'Male'),
(1002, 'Pam', 'Beasley', 30, 'Female'),
(1003, 'Dwight', 'Schrute', 29, 'Male'),
(1004, 'Angela', 'Martin', 31, 'Female'),
(1005, 'Toby', 'Flenderson', 32, 'Male'),
(1006, 'Michael', 'Scott', 35, 'Male'),
(1007, 'Meredith', 'Palmer', 32, 'Female'),
(1008, 'Stanley', 'Hudson', 38, 'Male'),
(1009, 'Kevin', 'Malone', 31, 'Male')
--Insert value in table 2 Query
INSERT INTO EmployeeSalary VALUES
(1001, 'Salesman', 45000),
(1002, 'Receptionist', 36000),
(1003, 'Salesman', 63000),
(1004, 'Accountant', 47000),
(1005, 'HR', 50000),
(1006, 'Regional Manager', 65000),
(1007, 'Supplier Relations', 41000),
(1008, 'Salesman', 48000),
(1009, 'Accountant', 42000)
/*
select statement
Top, Count, As, Distinct, Min, Max, Avg
*/
-- Top 5 row with all columns from EmployeeDemographics Table
SELECT Top 5 *
FROM EmployeeDemographics
-- Top 5 row with specific columns from EmployeeDemographics Table
SELECT Top 5 FirstName, LastName
From EmployeeDemographics
-- Count total number of row
SELECT COUNT(*)
From EmployeeDemographics
-- Count total number of row give name Total Rows
SELECT COUNT(*) AS 'TotalRows'
From EmployeeDemographics
-- DISTINCT Value of jobtitle From EmployeeSalary
SELECT DISTINCT(JobTitle)
From EmployeeSalary
-- MIN salary From EmployeeSalary
SELECT MIN(Salary)
FROM EmployeeSalary
-- MAX salary From EmployeeSalary
SELECT MAX(Salary)
FROM EmployeeSalary
--AVG salary From EmployeeSalary
SELECT AVG(Salary) AS 'AverageSalary'
FROM EmployeeSalary
/*
WHERE Statement use to get some specific results by applying some condition
WHERE Statement
=, <, >, <>, LIKE, IN, AND,OR, NULL, NOT NULL
*/
-- Get data whoes first name is Jim
SELECT *
FROM EmployeeDemographics
WHERE FirstName = 'Jim'
--Get Data whoes Age is grater than 30 And FirstName start with 'M'
SELECT *
FROM EmployeeDemographics
WHERE Age >30 And FirstName Like 'M%'
--Get Data whoes Age is not equal to 30
SELECT *
FROM EmployeeDemographics
WHERE Age <>30
--Get Data whoes salary is less than 45000
SELECT *
FROM EmployeeSalary
WHERE Salary <45000
-- Get data whoes name has 'e' letter and age in 30,31,32
SELECT *
FROM EmployeeDemographics
WHERE FirstName LIKE '%e%' AND Age IN (30,31,32)
--Get data whoes FirtName end with 'm' or LatName Start with 'H' And Age is NOT NULL value
SELECT *
FROM EmployeeDemographics
WHERE FirstName LIKE '%m' OR LastName Like 'H%' AND Age is NOT NULL
/*
Group by and order by
*/
-- count total number of female and male
SELECT Gender, Count(Gender) As 'Total'
From EmployeeDemographics Group by Gender
--Count how many number of male and female belong to which age group
SELECT Gender, Age, Count(Gender)
From EmployeeDemographics Group by Gender,Age
--now we order our result in ascending order
SELECT *
From EmployeeDemographics Where Age >30 Order by FirstName ASC
-- we can also use multiple columns to order our result
SELECT *
From EmployeeDemographics Order by Age ASC, FirstName DESC
--Use Where , Group by , Order by in single Query to get total number of person belongs to different age group and age group <40 and order this result into descending order
SELECT Age,Count(Age) As 'Total'
From EmployeeDemographics Where Age <40 Group by Age Order by 'Total' DESC
--we also use column number instead of column name
SELECT *
From EmployeeDemographics Order by 4,5 ASC