|
| 1 | +/* SQL Basics For Beginners */ |
| 2 | + |
| 3 | +/* |
| 4 | +TOPICS COVERED |
| 5 | +CREATE Statement |
| 6 | +INSERT Statement |
| 7 | +SELECT + FROM Statements |
| 8 | +WHERE Statement |
| 9 | +GROUP BY + ORDER BY Statements |
| 10 | +*/ |
| 11 | + |
| 12 | +-- Create two tables, EmployeeDemographics and EmployeeSalary |
| 13 | +CREATE TABLE EmployeeDemographics |
| 14 | +( |
| 15 | +EmployeeID int, |
| 16 | +FirstName varchar(50), |
| 17 | +LastName varchar(50), |
| 18 | +Age int, |
| 19 | +Gender varchar(50) |
| 20 | +) |
| 21 | + |
| 22 | +CREATE TABLE EmployeeSalary |
| 23 | +( |
| 24 | +EmployeeID int, |
| 25 | +JobTitle varchar(50), |
| 26 | +Salary int |
| 27 | +) |
| 28 | + |
| 29 | +-- Insert data into the tables |
| 30 | +INSERT INTO EmployeeDemographics |
| 31 | +VALUES |
| 32 | +(1001, 'Jim', 'Elliot', 30, 'Male'), |
| 33 | +(1002, 'Kimani', 'Eric', 35, 'Male'), |
| 34 | +(1003, 'Silas', 'Mugambi', 25, 'Male'), |
| 35 | +(1004, 'Stanly', 'Mwenda', 26, 'Male'), |
| 36 | +(1005, 'Tes', 'Kimani', 24, 'Female'), |
| 37 | +(1006, 'Douglas', 'Mochama', 23, 'Male'), |
| 38 | +(1007, 'Brevian', 'Mokeira', 19, 'Female'), |
| 39 | +(1008, 'Eddie', 'David', 24, 'Male'), |
| 40 | +(1009, 'Elius', 'Gitiye', 25, 'Male'), |
| 41 | +(1010, 'Jim', 'Murangiri', 27, 'Male'), |
| 42 | +(1011, 'Pauline', 'Ntela', 22, 'Female'), |
| 43 | +(1012, 'Tracy', 'Emelda', 24, 'Female'), |
| 44 | +(1013, 'Jeremy', 'Gakula', 29, 'Male'), |
| 45 | +(1014, 'Teresa', 'Ndune', 65, 'Female'), |
| 46 | +(1015, 'Georgina', 'Kendi', 44, 'Female') |
| 47 | + |
| 48 | +INSERT INTO EmployeeSalary |
| 49 | +VALUES |
| 50 | +(1001, 'Salesperson', 45000), |
| 51 | +(1002, 'Receptionist', 37000), |
| 52 | +(1003, 'Salesperson', 65000), |
| 53 | +(1004, 'Accountant', 49500), |
| 54 | +(1005, 'HR', 55000), |
| 55 | +(1006, 'Salesperson', 51000), |
| 56 | +(1007, 'Data Analyst', 85000), |
| 57 | +(1008, 'Software Engineer', 95000), |
| 58 | +(1009, 'Regional Manager', 125000), |
| 59 | +(1010, 'Supplier Relations', 43200), |
| 60 | +(1011, 'Salesperson', 71000), |
| 61 | +(1012, 'Salesperson', 39100), |
| 62 | +(1013, 'Accountant', 43500), |
| 63 | +(1014, 'Chef', 30000), |
| 64 | +(1015, 'Cleaner', 25000) |
| 65 | + |
| 66 | +-- Select all columns from the EmployeeDemographics table |
| 67 | +SELECT * |
| 68 | +FROM EmployeeDemographics |
| 69 | + |
| 70 | +-- Select only the FirstName and LastName columns from the EmployeeDemographics table |
| 71 | +SELECT FirstName, LastName |
| 72 | +FROM EmployeeDemographics |
| 73 | + |
| 74 | +-- Select the first 5 rows from the EmployeeDemographics table |
| 75 | +SELECT TOP 5 * |
| 76 | +FROM EmployeeDemographics |
| 77 | + |
| 78 | +-- Select distinct values from the Age column in the EmployeeDemographics table |
| 79 | +SELECT DISTINCT Age |
| 80 | +FROM EmployeeDemographics |
| 81 | + |
| 82 | +-- Count the number of rows in the EmployeeDemographics table |
| 83 | +SELECT COUNT(LastName) |
| 84 | +FROM EmployeeDemographics |
| 85 | + |
| 86 | +-- Count the number of rows in the EmployeeDemographics table and give the result a column alias |
| 87 | +SELECT COUNT(*) AS 'Total Rows' |
| 88 | +FROM EmployeeDemographics |
| 89 | + |
| 90 | +-- Count the number of rows in the EmployeeDemographics table where the last name is not null and give the result a column alias |
| 91 | +SELECT COUNT(LastName) AS 'LastName Count' |
| 92 | +FROM EmployeeDemographics |
| 93 | + |
| 94 | +-- Select all columns from the EmployeeSalary table |
| 95 | +SELECT * |
| 96 | +FROM EmployeeSalary |
| 97 | + |
| 98 | +-- Select the maximum value of Salary from the EmployeeSalary table |
| 99 | +SELECT MAX(Salary) |
| 100 | +FROM EmployeeSalary |
| 101 | + |
| 102 | +-- Select the minimum value of Salary from the EmployeeSalary table |
| 103 | +SELECT MIN(Salary) |
| 104 | +FROM EmployeeSalary |
| 105 | + |
| 106 | +-- Select the average value of Salary from the EmployeeSalary table |
| 107 | +SELECT AVG(Salary) |
| 108 | +FROM EmployeeSalary |
| 109 | + |
| 110 | +-- Select all columns from the EmployeeSalary table in the SQLTutorial database |
| 111 | +SELECT * |
| 112 | +FROM SQLTutorial.dbo.EmployeeSalary |
| 113 | + |
| 114 | +/* |
| 115 | +Where Statement |
| 116 | +=, <>, <, >, And, Or, Like, Null, Not Null, In |
| 117 | +*/ |
| 118 | + |
| 119 | +-- Select all columns from the EmployeeDemographics table where the first name is 'Jim' |
| 120 | +SELECT * |
| 121 | +FROM EmployeeDemographics |
| 122 | +WHERE FirstName = 'Jim' |
| 123 | + |
| 124 | +-- Select all columns from the EmployeeDemographics table where the first name is not 'Jim' |
| 125 | +SELECT * |
| 126 | +FROM EmployeeDemographics |
| 127 | +WHERE FirstName != 'Jim' |
| 128 | + |
| 129 | +-- Select all columns from the EmployeeDemographics table where the first name is not equal to 'Jim' |
| 130 | +SELECT * |
| 131 | +FROM EmployeeDemographics |
| 132 | +WHERE FirstName <> 'Jim' |
| 133 | + |
| 134 | +-- Select all columns from the EmployeeDemographics table where the age is greater than 27 |
| 135 | +SELECT * |
| 136 | +FROM EmployeeDemographics |
| 137 | +WHERE Age > 27 |
| 138 | + |
| 139 | +-- Select all columns from the EmployeeDemographics table where the age is greater than or equal to 27 |
| 140 | +SELECT * |
| 141 | +FROM EmployeeDemographics |
| 142 | +WHERE Age >= 27 |
| 143 | + |
| 144 | +-- Select all columns from the EmployeeDemographics table where the age is less than 29 |
| 145 | +SELECT * |
| 146 | +FROM EmployeeDemographics |
| 147 | +WHERE Age < 29 |
| 148 | + |
| 149 | +-- Select all columns from the EmployeeDemographics table where the age is less than or equal to 29 |
| 150 | +SELECT * |
| 151 | +FROM EmployeeDemographics |
| 152 | +WHERE Age <= 29 |
| 153 | + |
| 154 | +-- Select all columns from the EmployeeDemographics table where the age is less than or equal to 29 and the gender is male |
| 155 | +SELECT * |
| 156 | +FROM EmployeeDemographics |
| 157 | +WHERE Age <= 29 AND Gender = 'Male' |
| 158 | + |
| 159 | +-- Select all columns from the EmployeeDemographics table where the age is less than or equal to 29 or the gender is male |
| 160 | +SELECT * |
| 161 | +FROM EmployeeDemographics |
| 162 | +WHERE Age <= 29 OR Gender = 'Male' |
| 163 | + |
| 164 | +-- Select all columns from the EmployeeDemographics table where the last name starts with 'N' |
| 165 | +SELECT * |
| 166 | +FROM EmployeeDemographics |
| 167 | +WHERE LastName LIKE 'N%' -- LIKE is used to filter. In this case, you'll pull all the names starting with N. % sign follows the letter and is called wildcard |
| 168 | + |
| 169 | +-- Select all columns from the EmployeeDemographics table where the last name contains the letter 'N' |
| 170 | +SELECT * |
| 171 | +FROM EmployeeDemographics |
| 172 | +WHERE LastName LIKE '%N%' -- a letter between % sign implies the letter can be anywhere within the name |
| 173 | + |
| 174 | +-- Select all columns from the EmployeeDemographics table where the last name starts with 'N' and has 'U' somewhere in it |
| 175 | +SELECT * |
| 176 | +FROM EmployeeDemographics |
| 177 | +WHERE LastName LIKE 'N%U%' -- this specifies a name starting with N and has U somewhere in it |
| 178 | + |
| 179 | +/* |
| 180 | +Basically, LIKE is used to run wildcard |
| 181 | +*/ |
| 182 | + |
| 183 | +-- Select all columns from the EmployeeDemographics table where the first name is null |
| 184 | +SELECT * |
| 185 | +FROM EmployeeDemographics |
| 186 | +WHERE FirstName IS NULL |
| 187 | + |
| 188 | +-- Select all columns from the EmployeeDemographics table where the first name is not null |
| 189 | +SELECT * |
| 190 | +FROM EmployeeDemographics |
| 191 | +WHERE FirstName IS NOT NULL |
| 192 | + |
| 193 | +/* |
| 194 | +NULL is used to look wheather column is null or not. |
| 195 | +In the case above, it checks the column names FirstName |
| 196 | +*/ |
| 197 | + |
| 198 | +SELECT * |
| 199 | +FROM EmployeeDemographics |
| 200 | +WHERE FirstName = 'Jim' AND FirstName = 'Silas' |
| 201 | + |
| 202 | +SELECT * |
| 203 | +FROM EmployeeDemographics |
| 204 | +WHERE FirstName = 'Jim' OR FirstName = 'Silas' |
| 205 | + |
| 206 | +/* ALTERNATIVELY */ |
| 207 | +SELECT * |
| 208 | +FROM EmployeeDemographics |
| 209 | +WHERE FirstName IN ('Jim', 'Silas', 'Tracy') |
| 210 | + |
| 211 | + /* Explanation: |
| 212 | + This part is performing a basic SELECT operation with a WHERE clause to filter the results. It checks whether the FirstName column in the EmployeeDemographics table matches a certain value. The first query is checking if both 'Jim' and 'Silas' exist in the FirstName column. The second query is checking if either 'Jim' or 'Silas' exist in the FirstName column. The third query is an alternative way to perform the same operation using the IN operator. */ |
| 213 | + |
| 214 | + |
| 215 | +/* |
| 216 | +Part 4: |
| 217 | +Group By, Order By |
| 218 | + |
| 219 | +GROUP BY is similar to the DISTINCT Statement |
| 220 | +*/ |
| 221 | + |
| 222 | +SELECT * |
| 223 | +FROM EmployeeDemographics |
| 224 | + |
| 225 | +SELECT DISTINCT(Gender) |
| 226 | +FROM EmployeeDemographics /* this will return the very first unique value of female, and the first unique value of male */ |
| 227 | + |
| 228 | +SELECT Gender |
| 229 | +FROM EmployeeDemographics |
| 230 | +GROUP BY Gender/* this will return two values. That of female and male. However, all the females will be rolled up into the female raw and the same for the male */ |
| 231 | + |
| 232 | + /* MORE DETAILED ALTERNATIVE */ |
| 233 | + |
| 234 | + SELECT Gender, COUNT(Gender) |
| 235 | + FROM EmployeeDemographics |
| 236 | + GROUP BY Gender/* this will return two values. That of female and male. However, all the females will be rolled up into the female raw and the same for the male */ |
| 237 | + |
| 238 | +/* THIS EXAMPLE SHOW A TABLE with Head Count based on Gender and Age */ |
| 239 | +SELECT * |
| 240 | +FROM EmployeeDemographics |
| 241 | + |
| 242 | +SELECT Gender, Age, COUNT(Gender) AS 'Head Count' |
| 243 | +FROM EmployeeDemographics |
| 244 | +GROUP BY Gender, Age /* COUNT(Gender) column is not included here since it is a derived column. it is not an actual column */ |
| 245 | + |
| 246 | +SELECT Gender, COUNT(Gender) AS 'Head Count' |
| 247 | +FROM EmployeeDemographics |
| 248 | +WHERE Age > 30 |
| 249 | +GROUP BY Gender |
| 250 | + |
| 251 | +SELECT Gender, COUNT(Gender) AS 'Head Count' |
| 252 | +FROM EmployeeDemographics |
| 253 | +WHERE Age > 30 |
| 254 | +GROUP BY Gender |
| 255 | +ORDER BY [Head Count] |
| 256 | + |
| 257 | +SELECT Gender, COUNT(Gender) AS 'Head Count' |
| 258 | +FROM EmployeeDemographics |
| 259 | +WHERE Age > 30 |
| 260 | +GROUP BY Gender |
| 261 | +ORDER BY [Head Count] ASC /* Orders from smallest */ |
| 262 | + |
| 263 | +SELECT Gender, COUNT(Gender) AS 'Head Count' |
| 264 | +FROM EmployeeDemographics |
| 265 | +WHERE Age > 30 |
| 266 | +GROUP BY Gender |
| 267 | +ORDER BY [Head Count] DESC /* Orders from biggest */ |
| 268 | + |
| 269 | + /*Explanation: |
| 270 | + This part of the SQL code is focusing on GROUP BY and ORDER BY clauses, which allow for data aggregation and sorting. The first query is just selecting all data from the EmployeeDemographics table. The second query is using the DISTINCT keyword to get the unique values of Gender in the EmployeeDemographics table. The third query is grouping the data by Gender and returning the count of each gender. The fourth query is a more detailed version of the third query, including the count of each gender. The fifth query is selecting all data from the EmployeeDemographics table. The sixth query is grouping the data by Gender and Age, returning the count of each combination of gender and age. The seventh query is filtering the data to only include records where Age is greater than 30, then grouping the data by Gender and returning the count of each gender.*/ |
| 271 | + |
| 272 | + |
| 273 | +SELECT * |
| 274 | +FROM EmployeeDemographics |
| 275 | +ORDER BY Age |
| 276 | + |
| 277 | +SELECT * |
| 278 | +FROM EmployeeDemographics |
| 279 | +ORDER BY Age DESC |
| 280 | + |
| 281 | +SELECT * |
| 282 | +FROM EmployeeDemographics |
| 283 | +ORDER BY Age, Gender /* this orders by age, then under age, it orders by gender */ |
| 284 | + |
| 285 | +SELECT * |
| 286 | +FROM EmployeeDemographics |
| 287 | +ORDER BY Age DESC, Gender ASC /* this orders by age, then under age, it orders by gender */ |
| 288 | + |
| 289 | + /* you can also use number instead of column name. e.g. age is the 4th column in the table. therefore, I can use 4 to spacify the column */ |
| 290 | + SELECT * |
| 291 | + FROM EmployeeDemographics |
| 292 | + ORDER BY 4 DESC, 5 ASC /* this orders by age, then under age, it orders by gender */ |
0 commit comments