|
| 1 | +/************ 2) Simple Exercises *********/ |
| 2 | +/* List all emmployees */ |
| 3 | +SELECT * FROM employees LIMIT 10; |
| 4 | + |
| 5 | +/* How many departments? */ |
| 6 | +SELECT COUNT(*) FROM departments; |
| 7 | + |
| 8 | +/* How many times has Employee 10001 has a raise? */ |
| 9 | +SELECT COUNT(*) AS Number_of_raises FROM salaries |
| 10 | +WHERE emp_no = 10001; |
| 11 | + |
| 12 | +/* What title has 10006 has? */ |
| 13 | +SELECT title FROM titles |
| 14 | +WHERE emp_no = 10006; |
| 15 | + |
| 16 | +/***************** 5) Column Concat *****************/ |
| 17 | +SELECT CONCAT(emp_no, ' is a ', title) AS "EmpTitle" FROM titles |
| 18 | +LIMIT 5; |
| 19 | + |
| 20 | +SELECT emp_no, |
| 21 | + CONCAT(first_name, ' ', last_name) AS "Full Name" |
| 22 | +FROM employees |
| 23 | +LIMIT 5; |
| 24 | + |
| 25 | +/************** 6) Types of Functions in SQL *************/ |
| 26 | +/* |
| 27 | +Aggerate - operate on MANY records to produce ONE value, example: SUM of salaries |
| 28 | +Scalar - operate on EACH record Independently, example: CONCAT , it doesn't return result of concat values as one value. |
| 29 | +*/ |
| 30 | + |
| 31 | +/*********** 7) Aggregate Functions ************/ |
| 32 | +/* |
| 33 | +AVG() |
| 34 | +COUNT() |
| 35 | +MIN() |
| 36 | +MAX() |
| 37 | +SUM() |
| 38 | +*/ |
| 39 | +SELECT COUNT(*) FROM employees; |
| 40 | + |
| 41 | +SELECT MIN(emp_no) FROM employees; |
| 42 | + |
| 43 | +/* Get the highest salary avaliable */ |
| 44 | +SELECT MAX(salary) AS Max_Salary FROM salaries; |
| 45 | + |
| 46 | +/* Get the total amount of salaries paid */ |
| 47 | +SELECT SUM(salary) AS Total_Salary_Paid FROM salaries; |
| 48 | + |
| 49 | +/********** 9) Commenting your queries *******/ |
| 50 | +SELECT * FROM employees |
| 51 | +WHERE first_name='Mayumi' AND last_name='Schueller'; |
| 52 | + |
| 53 | +/*********** 11) Filtering Data ***********/ |
| 54 | +/* Get the list of all female employees */ |
| 55 | +SELECT * FROM employees |
| 56 | +WHERE gender = 'F' |
| 57 | +LIMIT 10; |
| 58 | + |
| 59 | +/********** 12) AND OR *************/ |
| 60 | +SELECT * |
| 61 | +FROM employees |
| 62 | +WHERE (first_name = 'Georgi' AND last_name='Facello' AND hire_date='1986-06-26') |
| 63 | +OR (first_name='Bezalel' AND last_name='Simmel'); |
| 64 | + |
| 65 | +/* 13) How many female customers do we have from state of Oregon or New York? */ |
| 66 | +SELECT COUNT(*) AS "NumberOfFemaleCustomers" |
| 67 | +FROM customers |
| 68 | +WHERE gender='F' AND (state LIKE 'OR' OR state LIKE 'NY'); |
| 69 | + |
| 70 | +/******** 15) NOT ***********/ |
| 71 | +/* How many customers aren't 55? */ |
| 72 | +SELECT COUNT(age) FROM customers |
| 73 | +WHERE NOT age=55; |
| 74 | + |
| 75 | + |
| 76 | +/***** 16) Comparison Operators *******/ |
| 77 | +/* 17) Exercises */ |
| 78 | +/* How many female customers do we have from the state of Oregon (OR)? */ |
| 79 | +SELECT COUNT(*) |
| 80 | +FROM customers |
| 81 | +WHERE gender='F' AND state='OR'; |
| 82 | + |
| 83 | +/* Who over the age of 44 has an income of 100 000 or more? */ |
| 84 | +SELECT * |
| 85 | +FROM customers |
| 86 | +WHERE age > 44 AND income=100000; |
| 87 | + |
| 88 | +/* Who between the ages of 30 and 50 has an income of less than 50 000? */ |
| 89 | +SELECT * |
| 90 | +FROM customers |
| 91 | +WHERE age BETWEEN 30 AND 50 |
| 92 | +AND income < 50000; |
| 93 | + |
| 94 | +/* What is the average income between the ages of 20 and 50? */ |
| 95 | +SELECT AVG(income) |
| 96 | +FROM customers |
| 97 | +WHERE age BETWEEN 20 AND 50; |
| 98 | + |
| 99 | + |
| 100 | +/***** 18) Logical Operators AND OR NOT *****/ |
| 101 | +/* |
| 102 | +Order of Operations |
| 103 | +FROM => WEHRE => SELECT |
| 104 | +*/ |
| 105 | + |
| 106 | +/***** 19) Operator Precedence ******/ |
| 107 | +/* |
| 108 | +(most importance to least importance) |
| 109 | +
|
| 110 | +Parentheses |
| 111 | +Multiplication / Division |
| 112 | +Subtraction / Addition |
| 113 | +NOT |
| 114 | +AND |
| 115 | +OR |
| 116 | +
|
| 117 | +If operators have equal precedence, then the operators are evaluated directionally. |
| 118 | +From Left to Right or Right to Left. |
| 119 | +
|
| 120 | +check Operators Precedence.png |
| 121 | +*/ |
| 122 | + |
| 123 | +SELECT state, gender FORM customers |
| 124 | +WHERE gender ='F' AND (state='NY' OR state='OR'); |
| 125 | + |
| 126 | +/****** 20) Operator Precedence 2 ********/ |
| 127 | +SELECT * |
| 128 | +FROM customers |
| 129 | +WHERE ( |
| 130 | + income > 10000 AND state='NY' |
| 131 | + OR ( |
| 132 | + (age > 20 AND age < 30) |
| 133 | + AND income <=20000 |
| 134 | + ) |
| 135 | +) AND gender='F' |
| 136 | + |
| 137 | +/*Select people either under 30 or over 50 with an income above 50000 that are from either Japan or Australia*/ |
| 138 | +SELECT * |
| 139 | +FROM customers |
| 140 | +WHERE (age < 30 OR age > 50) |
| 141 | +AND income > 50000 |
| 142 | +AND (country = 'Japan' OR country = 'Australia'); |
| 143 | + |
| 144 | +/*What was our total sales in June of 2004 for orders over 100 dollars?*/ |
| 145 | +SELECT SUM(totalamount) |
| 146 | +FROM orders |
| 147 | +WHERE totalamount > 100 |
| 148 | +AND orderdate='2004/06'; |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
0 commit comments