|
| 1 | +/****************************************/ |
| 2 | +/* 30 Simple SQL Interview Queries */ |
| 3 | +/****************************************/ |
| 4 | + |
| 5 | +/*1. Delete table Employee, Department and Company.*/ |
| 6 | +DROP TABLE IF EXISTS Employee; |
| 7 | +DROP TABLE IF EXISTS Department; |
| 8 | +DROP TABLE IF EXISTS Company; |
| 9 | + |
| 10 | +/* |
| 11 | +2. Create tables: |
| 12 | +
|
| 13 | +Employee with attributes (id, name, city, department, salary) |
| 14 | +Department with attributes (id, name) |
| 15 | +Company with attributes (id, name, revenue) |
| 16 | +*/ |
| 17 | +CREATE TABLE department( |
| 18 | + id INT PRIMARY KEY AUTO_INCREMENT, |
| 19 | + name VARCHAR(50) NOT NULL |
| 20 | +); |
| 21 | + |
| 22 | +CREATE TABLE company( |
| 23 | + id INT PRIMARY KEY AUTO_INCREMENT, |
| 24 | + name VARCHAR(100) NOT NULL, |
| 25 | + revenue INT |
| 26 | +); |
| 27 | + |
| 28 | +CREATE TABLE Employee( |
| 29 | + id INT PRIMARY KEY AUTO_INCREMENT, |
| 30 | + name VARCHAR(150) NOT NULL, |
| 31 | + city VARCHAR(150) NOT NULL, |
| 32 | + department_id INT NOT NULL, |
| 33 | + salary INT NOT NULL, |
| 34 | + FOREIGN KEY (department_id) REFERENCES department(id) |
| 35 | +); |
| 36 | + |
| 37 | +/* |
| 38 | +4. Add rows into Department table |
| 39 | +(1, 'IT'), |
| 40 | +(2, 'Management'), |
| 41 | +(3, 'IT'), |
| 42 | +(4, 'Support'); |
| 43 | +*/ |
| 44 | +INSERT INTO department(name) |
| 45 | +VALUES |
| 46 | +('IT'), |
| 47 | +('Management'), |
| 48 | +('IT'), |
| 49 | +('Support'); |
| 50 | + |
| 51 | +/* |
| 52 | +5. Add rows into Company table |
| 53 | +(1, 'IBM', 2000000), |
| 54 | +(2, 'GOOGLE', 9000000), |
| 55 | +(3, 'Apple', 10000000); |
| 56 | +*/ |
| 57 | +INSERT INTO company(name,revenue) |
| 58 | +VALUES |
| 59 | +('IBM', 2000000), |
| 60 | +('GOOGLE', 9000000), |
| 61 | +('Apple', 10000000); |
| 62 | + |
| 63 | +/* |
| 64 | +3.Add rows into employee table: |
| 65 | +(1, 'David', 'London', 'IT', 80000), |
| 66 | +(2, 'Emily', 'London', 'IT', 70000), |
| 67 | +(3, 'Peter', 'Paris', 'IT', 60000), |
| 68 | +(4, 'Ava', 'Paris', 'IT', 50000), |
| 69 | +(5, 'Penny', 'London', 'Management', 110000), |
| 70 | +(6, 'Jim', 'London', 'Management', 90000), |
| 71 | +(7, 'Amy', 'Rome', 'Support', 30000), |
| 72 | +(8, 'Cloe', 'London', 'IT', 110000); |
| 73 | +*/ |
| 74 | +INSERT INTO employee (name,city,department_id,salary) |
| 75 | +VALUES |
| 76 | +('David', 'London', 3, 80000), |
| 77 | +('Emily', 'London', 3, 70000), |
| 78 | +('Peter', 'Paris', 3, 60000), |
| 79 | +('Ava', 'Paris', 3, 50000), |
| 80 | +('Penny', 'London', 2, 110000), |
| 81 | +('Jim', 'London', 2, 90000), |
| 82 | +('Amy', 'Rome', 4, 30000), |
| 83 | +('Cloe', 'London', 3, 110000); |
| 84 | + |
| 85 | +/* |
| 86 | +6. Query all rows from Department table |
| 87 | +*/ |
| 88 | +SELECT * FROM department; |
| 89 | + |
| 90 | +/* |
| 91 | +7. Change the name of department with id = 1 to 'Management' |
| 92 | +*/ |
| 93 | +UPDATE department |
| 94 | +SET name = 'Management' |
| 95 | +WHERE id = 1; |
| 96 | + |
| 97 | +/* |
| 98 | +8. Delete employees with salary greater than 100 000 |
| 99 | +*/ |
| 100 | +DELETE FROM employee |
| 101 | +WHERE salary > 100000; |
| 102 | + |
| 103 | +/* |
| 104 | +9. Query the names of companies |
| 105 | +*/ |
| 106 | +SELECT name FROM company; |
| 107 | + |
| 108 | +/* |
| 109 | +10. Query the name and city of every employee |
| 110 | +*/ |
| 111 | +SELECT name, city |
| 112 | +FROM employee; |
| 113 | + |
| 114 | +/* |
| 115 | +11. Query all companies with revenue greater than 5 000 000 |
| 116 | +*/ |
| 117 | +SELECT * FROM company |
| 118 | +WHERE revenue > 5000000; |
| 119 | + |
| 120 | +/* |
| 121 | +12. Query all companies with revenue smaller than 5 000 000 |
| 122 | +*/ |
| 123 | +SELECT * FROM company |
| 124 | +WHERE revenue < 5000000; |
| 125 | + |
| 126 | +/* |
| 127 | +13. Query all companies with revenue smaller than 5 000 000, but you cannot use the '<' operator |
| 128 | +*/ |
| 129 | +SELECT * FROM company |
| 130 | +ORDER BY revenue |
| 131 | +LIMIT 1; |
| 132 | + |
| 133 | +/*version 2*/ |
| 134 | +SELECT * FROM company |
| 135 | +WHERE NOT revenue >= 5000000; |
| 136 | + |
| 137 | +/* |
| 138 | +14. Query all employees with salary greater than 50 000 and smaller than 70 000 |
| 139 | +*/ |
| 140 | +SELECT * FROM employee |
| 141 | +WHERE salary BETWEEN 50000 AND 70000; |
| 142 | + |
| 143 | +/* |
| 144 | +15. Query all employees with salary greater than 50 000 and smaller than 70 000, but you cannot use BETWEEN |
| 145 | +*/ |
| 146 | +SELECT * FROM employee |
| 147 | +WHERE salary >= 50000 AND salary <= 70000; |
| 148 | + |
| 149 | +/* |
| 150 | +16. Query all employees with salary equal to 80 000 |
| 151 | +*/ |
| 152 | +SELECT * FROM employee |
| 153 | +WHERE salary = 80000; |
| 154 | + |
| 155 | +/* |
| 156 | +17. Query all employees with salary not equal to 80 000 |
| 157 | +*/ |
| 158 | +SELECT * FROM employee |
| 159 | +WHERE salary <> 80000; |
| 160 | + |
| 161 | +/* |
| 162 | +18. Query all names of employees with salary greater than 70 000 together with employees who work on the 'IT' department. |
| 163 | +*/ |
| 164 | +SELECT name FROM employee |
| 165 | +WHERE salary > 70000 |
| 166 | +OR department_id IN ( |
| 167 | + SELECT id FROM department |
| 168 | + WHERE name = 'IT' |
| 169 | +); |
| 170 | + |
| 171 | +/* |
| 172 | +19. Query all employees that work in city that starts with 'L' |
| 173 | +*/ |
| 174 | +SELECT * FROM employee |
| 175 | +WHERE city LIKE 'L%'; |
| 176 | + |
| 177 | +/* |
| 178 | +20. Query all employees that work in city that starts with 'L' or ends with 's' |
| 179 | +*/ |
| 180 | +SELECT * FROM employee |
| 181 | +WHERE city LIKE 'L%' OR city LIKE '%s'; |
| 182 | + |
| 183 | +/* |
| 184 | +21. Query all employees that work in city with 'o' somewhere in the middle |
| 185 | +*/ |
| 186 | +SELECT * FROM employee |
| 187 | +WHERE city LIKE '%o%'; |
| 188 | + |
| 189 | +/* |
| 190 | +22. Query all departments (each name only once) |
| 191 | +*/ |
| 192 | +SELECT DISTINCT name FROM department; |
| 193 | + |
| 194 | +/* |
| 195 | +22. Query names of all employees together with id of department they work in, but you cannot use JOIN |
| 196 | +*/ |
| 197 | +SELECT emp.name,dep.id,dep.name |
| 198 | +FROM employee emp, department dep |
| 199 | +WHERE emp.department_id = dep.id |
| 200 | +ORDER BY emp.name, dep.id; |
| 201 | + |
| 202 | +/* |
| 203 | +23. Query names of all employees together with id of department they work in, using JOIN |
| 204 | +*/ |
| 205 | +SELECT emp.name,dep.id,dep.name |
| 206 | +FROM employee emp |
| 207 | +JOIN department dep |
| 208 | +ON emp.department_id = dep.id |
| 209 | +ORDER BY emp.name, dep.id; |
| 210 | + |
| 211 | +/* |
| 212 | +24. Query name of every company together with every department |
| 213 | +Personal thoughts: It is kinda weird question, as there is no relationship between company and departement |
| 214 | +*/ |
| 215 | +SELECT com.name,dep.name |
| 216 | +FROM company com, department dep |
| 217 | +ORDER BY com.name; |
| 218 | + |
| 219 | +/* |
| 220 | +25. Query name of every company together with departments without the 'Support' department |
| 221 | +*/ |
| 222 | +SELECT com.name,dep.name |
| 223 | +FROM company com, department dep |
| 224 | +WHERE dep.name NOT LIKE 'Support' |
| 225 | +ORDER BY com.name; |
| 226 | + |
| 227 | +/* |
| 228 | +26. Query employee name together with the department name that they are not working in |
| 229 | +*/ |
| 230 | +SELECT emp.name, dep.name |
| 231 | +FROM employee emp, department dep |
| 232 | +WHERE emp.department_id <> dep.id; |
| 233 | + |
| 234 | +/* |
| 235 | +27. Query company name together with other companies names |
| 236 | +LIKE: |
| 237 | +GOOGLE Apple |
| 238 | +GOOGLE IBM |
| 239 | +Apple IBM |
| 240 | +... |
| 241 | +*/ |
| 242 | +SELECT com1.name, com2.name |
| 243 | +FROM company com1, company com2 |
| 244 | +WHERE com1.name <> com2.name |
| 245 | +ORDER BY com1.name,com2.name; |
| 246 | + |
| 247 | + |
| 248 | +/* |
| 249 | +28. Query employee names with salary smaller than 80 000 without using NOT and < |
| 250 | +NOTE: for POSTGRESQL only. Mysql doesn't support except |
| 251 | +*/ |
| 252 | +SELECT e1.name FROM employee e1 |
| 253 | +EXCEPT |
| 254 | +SELECT e2.name FROM employee e2 WHERE e2.salary >= 80000; |
| 255 | + |
| 256 | + |
| 257 | +/* |
| 258 | +29.Query names of every company and change the name of column to 'Company' |
| 259 | +*/ |
| 260 | +SELECT name AS Company |
| 261 | +FROM company; |
| 262 | + |
| 263 | +/* |
| 264 | +30. Query all employees that work in same department as Peter |
| 265 | +*/ |
| 266 | +SELECT * FROM employee |
| 267 | +WHERE department_id IN( |
| 268 | + SELECT department_id FROM employee |
| 269 | + WHERE name LIKE 'Peter' |
| 270 | +) |
| 271 | +AND name NOT LIKE 'Peter'; |
0 commit comments