123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. Your MySQL connection id is 7
  2. Server version: 5.7.22 Homebrew
  3. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  4. Oracle is a registered trademark of Oracle Corporation and/or its
  5. affiliates. Other names may be trademarks of their respective
  6. owners.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. mysql> show tables
  9. -> ;
  10. +---------------+
  11. | Tables_in_db2 |
  12. +---------------+
  13. | homes |
  14. | movies |
  15. | people |
  16. +---------------+
  17. 3 rows in set (0.00 sec)
  18. mysql> exit
  19. Bye
  20. zipcoders-MacBook-Pro-45:ZCW-Money-Converter-BlueJ randallg$ mysql -u root -p db2
  21. Enter password:
  22. Reading table information for completion of table and column names
  23. You can turn off this feature to get a quicker startup with -A
  24. Welcome to the MySQL monitor. Commands end with ; or \g.
  25. Your MySQL connection id is 8
  26. Server version: 5.7.22 Homebrew
  27. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  28. Oracle is a registered trademark of Oracle Corporation and/or its
  29. affiliates. Other names may be trademarks of their respective
  30. owners.
  31. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  32. mysql> show tables
  33. -> ;
  34. +---------------+
  35. | Tables_in_db2 |
  36. +---------------+
  37. | homes |
  38. | movies |
  39. | people |
  40. +---------------+
  41. 3 rows in set (0.00 sec)
  42. mysql> select * from people
  43. -> ;
  44. +----+------------+-----------+----------+------------+---------+
  45. | id | first_name | last_name | mobile | birthday | home_id |
  46. +----+------------+-----------+----------+------------+---------+
  47. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  48. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  49. | 3 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
  50. | 4 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
  51. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  52. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  53. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  54. +----+------------+-----------+----------+------------+---------+
  55. 7 rows in set (0.01 sec)
  56. mysql> UPDATE people SET mobile = '333-3333', last_name = 'Johnson'
  57. -> WHERE first_name = 'Noelle' OR first_name = 'Raj';
  58. Query OK, 2 rows affected (0.01 sec)
  59. Rows matched: 2 Changed: 2 Warnings: 0
  60. mysql> select * from people
  61. -> ;
  62. +----+------------+-----------+----------+------------+---------+
  63. | id | first_name | last_name | mobile | birthday | home_id |
  64. +----+------------+-----------+----------+------------+---------+
  65. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  66. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  67. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  68. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  69. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  70. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  71. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  72. +----+------------+-----------+----------+------------+---------+
  73. 7 rows in set (0.00 sec)
  74. mysql> select sum(id), avg(id) from people;
  75. +---------+---------+
  76. | sum(id) | avg(id) |
  77. +---------+---------+
  78. | 28 | 4.0000 |
  79. +---------+---------+
  80. 1 row in set (0.01 sec)
  81. mysql> select cout(homenumber) from homes;
  82. ERROR 1305 (42000): FUNCTION db2.cout does not exist
  83. mysql> show tables
  84. -> ;
  85. +---------------+
  86. | Tables_in_db2 |
  87. +---------------+
  88. | homes |
  89. | movies |
  90. | people |
  91. +---------------+
  92. 3 rows in set (0.01 sec)
  93. mysql> select * from homes
  94. -> ;
  95. +----+----------------------------------------+------------+
  96. | id | address | homenumber |
  97. +----+----------------------------------------+------------+
  98. | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  99. | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  100. | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  101. | 4 | 234 High Street, PA 19159 | 267-3940 |
  102. +----+----------------------------------------+------------+
  103. 4 rows in set (0.00 sec)
  104. mysql> select count(homenumber) from homes;
  105. +-------------------+
  106. | count(homenumber) |
  107. +-------------------+
  108. | 4 |
  109. +-------------------+
  110. 1 row in set (0.00 sec)
  111. mysql> select count(*) from homes
  112. -> ;
  113. +----------+
  114. | count(*) |
  115. +----------+
  116. | 4 |
  117. +----------+
  118. 1 row in set (0.01 sec)
  119. mysql> select count(distinct last_name) from people;
  120. +---------------------------+
  121. | count(distinct last_name) |
  122. +---------------------------+
  123. | 4 |
  124. +---------------------------+
  125. 1 row in set (0.00 sec)
  126. mysql> select * from people
  127. -> ;
  128. +----+------------+-----------+----------+------------+---------+
  129. | id | first_name | last_name | mobile | birthday | home_id |
  130. +----+------------+-----------+----------+------------+---------+
  131. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  132. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  133. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  134. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  135. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  136. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  137. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  138. +----+------------+-----------+----------+------------+---------+
  139. 7 rows in set (0.00 sec)
  140. mysql> select sum(id) as sum, avg(id) as avg from people;
  141. +------+--------+
  142. | sum | avg |
  143. +------+--------+
  144. | 28 | 4.0000 |
  145. +------+--------+
  146. 1 row in set (0.00 sec)
  147. mysql> select sum(id), avg(id) from people;
  148. +---------+---------+
  149. | sum(id) | avg(id) |
  150. +---------+---------+
  151. | 28 | 4.0000 |
  152. +---------+---------+
  153. 1 row in set (0.00 sec)
  154. mysql> select min(birthday) from people;
  155. +---------------+
  156. | min(birthday) |
  157. +---------------+
  158. | 1954-12-07 |
  159. +---------------+
  160. 1 row in set (0.01 sec)
  161. mysql> Select upper(first_name), lower(last_name) from people;
  162. +-------------------+------------------+
  163. | upper(first_name) | lower(last_name) |
  164. +-------------------+------------------+
  165. | TONY | smith |
  166. | SHEERI | carbral |
  167. | RAJ | johnson |
  168. | NOELLE | johnson |
  169. | THOMAS | smith |
  170. | JANE | smith |
  171. | DOUG | brown |
  172. +-------------------+------------------+
  173. 7 rows in set (0.00 sec)
  174. mysql> select replace(last_name, 'a', '1') from people;
  175. +------------------------------+
  176. | replace(last_name, 'a', '1') |
  177. +------------------------------+
  178. | Smith |
  179. | C1rbr1l |
  180. | Johnson |
  181. | Johnson |
  182. | Smith |
  183. | Smith |
  184. | Brown |
  185. +------------------------------+
  186. 7 rows in set (0.00 sec)
  187. mysql> select last_name from people;
  188. +-----------+
  189. | last_name |
  190. +-----------+
  191. | Smith |
  192. | Carbral |
  193. | Johnson |
  194. | Johnson |
  195. | Smith |
  196. | Smith |
  197. | Brown |
  198. +-----------+
  199. 7 rows in set (0.00 sec)
  200. mysql> select * from people;
  201. +----+------------+-----------+----------+------------+---------+
  202. | id | first_name | last_name | mobile | birthday | home_id |
  203. +----+------------+-----------+----------+------------+---------+
  204. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  205. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  206. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  207. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  208. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  209. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  210. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  211. +----+------------+-----------+----------+------------+---------+
  212. 7 rows in set (0.00 sec)
  213. mysql> INSERT INTO people (first_name, last_name, mobile)
  214. -> VALUES ('Otto', 'Von Count', '656-6548');
  215. Query OK, 1 row affected (0.01 sec)
  216. mysql> select * from people;
  217. +----+------------+-----------+----------+------------+---------+
  218. | id | first_name | last_name | mobile | birthday | home_id |
  219. +----+------------+-----------+----------+------------+---------+
  220. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  221. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  222. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  223. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  224. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  225. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  226. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  227. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  228. +----+------------+-----------+----------+------------+---------+
  229. 8 rows in set (0.00 sec)
  230. mysql> SELECT CONCAT(first_name, last_name) FROM people
  231. -> WHERE last_name = 'Smith';
  232. +-------------------------------+
  233. | CONCAT(first_name, last_name) |
  234. +-------------------------------+
  235. | TonySmith |
  236. | ThomasSmith |
  237. | JaneSmith |
  238. +-------------------------------+
  239. 3 rows in set (0.00 sec)
  240. mysql> SELECT CONCAT(first_name, ' ', last_name)
  241. -> FROM people
  242. -> WHERE last_name = 'Smith';
  243. +------------------------------------+
  244. | CONCAT(first_name, ' ', last_name) |
  245. +------------------------------------+
  246. | Tony Smith |
  247. | Thomas Smith |
  248. | Jane Smith |
  249. +------------------------------------+
  250. 3 rows in set (0.01 sec)
  251. mysql> SELECT CONCAT_WS(' ',first_name, last_name, mobile)
  252. -> FROM people WHERE last_name= 'Smith';
  253. +----------------------------------------------+
  254. | CONCAT_WS(' ',first_name, last_name, mobile) |
  255. +----------------------------------------------+
  256. | Tony Smith 152-9854 |
  257. | Thomas Smith 152-9854 |
  258. | Jane Smith 152-9854 |
  259. +----------------------------------------------+
  260. 3 rows in set (0.00 sec)
  261. mysql> SELECT homenumber, LEFT(homenumber, 3), RIGHT(homenumber, 2) FROM homes;
  262. +------------+---------------------+----------------------+
  263. | homenumber | LEFT(homenumber, 3) | RIGHT(homenumber, 2) |
  264. +------------+---------------------+----------------------+
  265. | 565-6895 | 565 | 95 |
  266. | 454-4544 | 454 | 44 |
  267. | 985-4515 | 985 | 15 |
  268. | 267-3940 | 267 | 40 |
  269. +------------+---------------------+----------------------+
  270. 4 rows in set (0.00 sec)
  271. mysql> select * from homes
  272. -> ;
  273. +----+----------------------------------------+------------+
  274. | id | address | homenumber |
  275. +----+----------------------------------------+------------+
  276. | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  277. | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  278. | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  279. | 4 | 234 High Street, PA 19159 | 267-3940 |
  280. +----+----------------------------------------+------------+
  281. 4 rows in set (0.00 sec)
  282. mysql> SELECT LENGTH(address), CHAR_LENGTH(address) FROM homes;
  283. +-----------------+----------------------+
  284. | LENGTH(address) | CHAR_LENGTH(address) |
  285. +-----------------+----------------------+
  286. | 36 | 36 |
  287. | 33 | 33 |
  288. | 38 | 38 |
  289. | 26 | 26 |
  290. +-----------------+----------------------+
  291. 4 rows in set (0.00 sec)
  292. mysql> SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND
  293. -> ;
  294. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
  295. mysql> select * from people;
  296. +----+------------+-----------+----------+------------+---------+
  297. | id | first_name | last_name | mobile | birthday | home_id |
  298. +----+------------+-----------+----------+------------+---------+
  299. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  300. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  301. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  302. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  303. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  304. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  305. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  306. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  307. +----+------------+-----------+----------+------------+---------+
  308. 8 rows in set (0.00 sec)
  309. mysql> SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
  310. +------------+-----------+----------------+
  311. | first_name | last_name | YEAR(birthday) |
  312. +------------+-----------+----------------+
  313. | Tony | Smith | 1973 |
  314. | Raj | Johnson | 1980 |
  315. | Thomas | Smith | 1987 |
  316. +------------+-----------+----------------+
  317. 3 rows in set (0.00 sec)
  318. mysql> SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
  319. +------------+------------+
  320. | first_name | birthday |
  321. +------------+------------+
  322. | Sheeri | 1970-02-23 |
  323. | Raj | 1980-08-31 |
  324. | Thomas | 1987-07-06 |
  325. +------------+------------+
  326. 3 rows in set (0.00 sec)
  327. mysql> SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
  328. +------------+------------+
  329. | first_name | birthday |
  330. +------------+------------+
  331. | Raj | 1980-08-31 |
  332. | Noelle | 1960-07-06 |
  333. | Thomas | 1987-07-06 |
  334. +------------+------------+
  335. 3 rows in set (0.00 sec)
  336. mysql> SELECT first_name FROM people WHERE RIGHT(first_name,1)='e';
  337. +------------+
  338. | first_name |
  339. +------------+
  340. | Noelle |
  341. | Jane |
  342. +------------+
  343. 2 rows in set (0.00 sec)
  344. mysql> select * from people;
  345. +----+------------+-----------+----------+------------+---------+
  346. | id | first_name | last_name | mobile | birthday | home_id |
  347. +----+------------+-----------+----------+------------+---------+
  348. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  349. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  350. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  351. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  352. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  353. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  354. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  355. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  356. +----+------------+-----------+----------+------------+---------+
  357. 8 rows in set (0.00 sec)
  358. mysql> SELECT first_name FROM people WHERE first_name LIKE '%j';
  359. +------------+
  360. | first_name |
  361. +------------+
  362. | Raj |
  363. +------------+
  364. 1 row in set (0.00 sec)
  365. mysql> SELECT first_name FROM people WHERE first_name LIKE 'j';
  366. Empty set (0.00 sec)
  367. mysql> SELECT first_name FROM people WHERE first_name LIKE 'J';
  368. Empty set (0.00 sec)
  369. mysql> SELECT first_name FROM people WHERE first_name LIKE '%J;
  370. '> ;
  371. '> ;
  372. '> exit
  373. '> SELECT first_name FROM people WHERE first_name LIKE '%o%';
  374. '> ';
  375. mysql> SELECT first_name FROM people WHERE first_name LIKE '%J';
  376. +------------+
  377. | first_name |
  378. +------------+
  379. | Raj |
  380. +------------+
  381. 1 row in set (0.00 sec)
  382. mysql> SELECT first_name FROM people WHERE first_name LIKE '%o%';
  383. +------------+
  384. | first_name |
  385. +------------+
  386. | Tony |
  387. | Noelle |
  388. | Thomas |
  389. | Doug |
  390. | Otto |
  391. +------------+
  392. 5 rows in set (0.00 sec)
  393. mysql> SELECT first_name FROM people WHERE first_name LIKE 'J%';
  394. +------------+
  395. | first_name |
  396. +------------+
  397. | Jane |
  398. +------------+
  399. 1 row in set (0.00 sec)
  400. mysql> SELECT first_name FROM people WHERE first_name LIKE 'J*';
  401. Empty set (0.00 sec)
  402. mysql> SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
  403. +------------+
  404. | first_name |
  405. +------------+
  406. | Sheeri |
  407. | Raj |
  408. | Jane |
  409. +------------+
  410. 3 rows in set (0.00 sec)
  411. mysql> select * from people;
  412. +----+------------+-----------+----------+------------+---------+
  413. | id | first_name | last_name | mobile | birthday | home_id |
  414. +----+------------+-----------+----------+------------+---------+
  415. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  416. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  417. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  418. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  419. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  420. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  421. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  422. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  423. +----+------------+-----------+----------+------------+---------+
  424. 8 rows in set (0.00 sec)
  425. mysql> SELECT COUNT(*) FROM people;
  426. +----------+
  427. | COUNT(*) |
  428. +----------+
  429. | 8 |
  430. +----------+
  431. 1 row in set (0.00 sec)
  432. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
  433. +-----------+----------+
  434. | last_name | COUNT(*) |
  435. +-----------+----------+
  436. | Brown | 1 |
  437. | Carbral | 1 |
  438. | Johnson | 2 |
  439. | Smith | 3 |
  440. | Von Count | 1 |
  441. +-----------+----------+
  442. 5 rows in set (0.01 sec)
  443. mysql> SELECT last_name, GROUP_CONCAT(mobile) FROM people GROUP BY last_name;
  444. +-----------+----------------------------+
  445. | last_name | GROUP_CONCAT(mobile) |
  446. +-----------+----------------------------+
  447. | Brown | 466-6241 |
  448. | Carbral | 230-4233 |
  449. | Johnson | 333-3333,333-3333 |
  450. | Smith | 152-9854,152-9854,152-9854 |
  451. | Von Count | 656-6548 |
  452. +-----------+----------------------------+
  453. 5 rows in set (0.00 sec)
  454. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
  455. +-----------+----------------------------------------+
  456. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  457. +-----------+----------------------------------------+
  458. | Brown | 466-6241 |
  459. | Carbral | 230-4233 |
  460. | Johnson | 333-3333 and 333-3333 |
  461. | Smith | 152-9854 and 152-9854 and 152-9854 |
  462. | Von Count | 656-6548 |
  463. +-----------+----------------------------------------+
  464. 5 rows in set (0.00 sec)
  465. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name HAVING COUNT(*)>1;
  466. +-----------+----------------------------------------+
  467. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  468. +-----------+----------------------------------------+
  469. | Johnson | 333-3333 and 333-3333 |
  470. | Smith | 152-9854 and 152-9854 and 152-9854 |
  471. +-----------+----------------------------------------+
  472. 2 rows in set (0.00 sec)
  473. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name HAVING COUNT(*)>1;
  474. +-----------+----------------------------------------+
  475. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  476. +-----------+----------------------------------------+
  477. | Johnson | 333-3333 and 333-3333 |
  478. | Smith | 152-9854 and 152-9854 and 152-9854 |
  479. +-----------+----------------------------------------+
  480. 2 rows in set (0.00 sec)
  481. mysql> SELECT first_name, birthday FROM people ORDER BY birthday;
  482. +------------+------------+
  483. | first_name | birthday |
  484. +------------+------------+
  485. | Otto | NULL |
  486. | Doug | 1954-12-07 |
  487. | Noelle | 1960-07-06 |
  488. | Sheeri | 1970-02-23 |
  489. | Tony | 1973-01-23 |
  490. | Raj | 1980-08-31 |
  491. | Thomas | 1987-07-06 |
  492. | Jane | 1987-12-06 |
  493. +------------+------------+
  494. 8 rows in set (0.00 sec)
  495. mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC;
  496. +------------+------------+
  497. | first_name | birthday |
  498. +------------+------------+
  499. | Jane | 1987-12-06 |
  500. | Thomas | 1987-07-06 |
  501. | Raj | 1980-08-31 |
  502. | Tony | 1973-01-23 |
  503. | Sheeri | 1970-02-23 |
  504. | Noelle | 1960-07-06 |
  505. | Doug | 1954-12-07 |
  506. | Otto | NULL |
  507. +------------+------------+
  508. 8 rows in set (0.00 sec)
  509. mysql> SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
  510. +------------+-----------+
  511. | first_name | last_name |
  512. +------------+-----------+
  513. | Doug | Brown |
  514. | Sheeri | Carbral |
  515. | Noelle | Johnson |
  516. | Raj | Johnson |
  517. | Jane | Smith |
  518. | Thomas | Smith |
  519. | Tony | Smith |
  520. | Otto | Von Count |
  521. +------------+-----------+
  522. 8 rows in set (0.00 sec)
  523. mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
  524. +------------+------------+
  525. | first_name | birthday |
  526. +------------+------------+
  527. | Jane | 1987-12-06 |
  528. | Thomas | 1987-07-06 |
  529. | Raj | 1980-08-31 |
  530. +------------+------------+
  531. 3 rows in set (0.00 sec)
  532. mysql> SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
  533. +------------+----------+------------+
  534. | first_name | mon | birthday |
  535. +------------+----------+------------+
  536. | Otto | NULL | NULL |
  537. | Tony | January | 1973-01-23 |
  538. | Sheeri | February | 1970-02-23 |
  539. | Noelle | July | 1960-07-06 |
  540. | Thomas | July | 1987-07-06 |
  541. | Raj | August | 1980-08-31 |
  542. | Jane | December | 1987-12-06 |
  543. | Doug | December | 1954-12-07 |
  544. +------------+----------+------------+
  545. 8 rows in set (0.01 sec)
  546. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
  547. +-----------+----------+
  548. | last_name | COUNT(*) |
  549. +-----------+----------+
  550. | Brown | 1 |
  551. | Carbral | 1 |
  552. | Johnson | 2 |
  553. | Smith | 3 |
  554. | Von Count | 1 |
  555. +-----------+----------+
  556. 5 rows in set (0.00 sec)
  557. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name ORDER BY NULL;
  558. +-----------+----------+
  559. | last_name | COUNT(*) |
  560. +-----------+----------+
  561. | Smith | 3 |
  562. | Carbral | 1 |
  563. | Johnson | 2 |
  564. | Brown | 1 |
  565. | Von Count | 1 |
  566. +-----------+----------+
  567. 5 rows in set (0.00 sec)
  568. mysql> INSERT INTO people (first_name, last_name, birthday, home_id)
  569. -> VALUES ('John', 'Smith', '1998-04-07', 4),
  570. -> ('Maya', 'Wasserman' , NULL, 4),
  571. -> ('Paul', 'Thompson', '1996-05-27', 1);
  572. Query OK, 3 rows affected (0.01 sec)
  573. Records: 3 Duplicates: 0 Warnings: 0
  574. mysql> select * from people;
  575. +----+------------+-----------+----------+------------+---------+
  576. | id | first_name | last_name | mobile | birthday | home_id |
  577. +----+------------+-----------+----------+------------+---------+
  578. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  579. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  580. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  581. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  582. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  583. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  584. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  585. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  586. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  587. | 15 | Maya | Wasserman | NULL | NULL | 4 |
  588. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  589. +----+------------+-----------+----------+------------+---------+
  590. 11 rows in set (0.00 sec)
  591. mysql> DELETE FROM people WHERE first_name='Maya';
  592. Query OK, 1 row affected (0.00 sec)
  593. mysql> select * from people;
  594. +----+------------+-----------+----------+------------+---------+
  595. | id | first_name | last_name | mobile | birthday | home_id |
  596. +----+------------+-----------+----------+------------+---------+
  597. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  598. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  599. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  600. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  601. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  602. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  603. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  604. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  605. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  606. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  607. +----+------------+-----------+----------+------------+---------+
  608. 10 rows in set (0.00 sec)
  609. mysql> INSERT INTO people (first_name, last_name, birthday)
  610. -> VALUES ('Eli', 'Kramer', '1984-01-15');
  611. Query OK, 1 row affected (0.01 sec)
  612. mysql> select * from people;
  613. +----+------------+-----------+----------+------------+---------+
  614. | id | first_name | last_name | mobile | birthday | home_id |
  615. +----+------------+-----------+----------+------------+---------+
  616. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  617. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  618. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  619. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  620. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  621. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  622. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  623. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  624. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  625. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  626. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  627. +----+------------+-----------+----------+------------+---------+
  628. 11 rows in set (0.00 sec)
  629. mysql> select * from homes;
  630. +----+----------------------------------------+------------+
  631. | id | address | homenumber |
  632. +----+----------------------------------------+------------+
  633. | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  634. | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  635. | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  636. | 4 | 234 High Street, PA 19159 | 267-3940 |
  637. +----+----------------------------------------+------------+
  638. 4 rows in set (0.01 sec)
  639. mysql> SELECT p.first_name, h.address
  640. -> FROM people p
  641. -> INNER JOIN homes h on (p.home_id = h.id);
  642. +------------+----------------------------------------+
  643. | first_name | address |
  644. +------------+----------------------------------------+
  645. | Noelle | 36 E. Bayberry Rd.Savannah, GA 31404 |
  646. | Thomas | 36 E. Bayberry Rd.Savannah, GA 31404 |
  647. | Paul | 36 E. Bayberry Rd.Savannah, GA 31404 |
  648. | Sheeri | 11 Essex Dr.Farmingdale, NY 11735 |
  649. | Raj | 920 Arlington Street Clifton, NJ 07011 |
  650. | Jane | 920 Arlington Street Clifton, NJ 07011 |
  651. | Doug | 920 Arlington Street Clifton, NJ 07011 |
  652. | John | 234 High Street, PA 19159 |
  653. +------------+----------------------------------------+
  654. 8 rows in set (0.01 sec)
  655. mysql> SELECT first_name, last_name
  656. -> FROM people p
  657. -> INNER JOIN homes h on (p.home_id = h.id)
  658. -> WHERE p.HOME_ID = 1;
  659. +------------+-----------+
  660. | first_name | last_name |
  661. +------------+-----------+
  662. | Noelle | Johnson |
  663. | Thomas | Smith |
  664. | Paul | Thompson |
  665. +------------+-----------+
  666. 3 rows in set (0.01 sec)
  667. mysql> SELECT p.*, h.address, h.homenumber
  668. -> FROM people p
  669. -> INNER JOIN homes h on (p.home_id = h.id)
  670. -> WHERE p.first_name LIKE '%e%';
  671. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  672. | id | first_name | last_name | mobile | birthday | home_id | address | homenumber |
  673. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  674. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  675. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  676. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  677. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  678. 3 rows in set (0.00 sec)
  679. mysql> select * from people;
  680. +----+------------+-----------+----------+------------+---------+
  681. | id | first_name | last_name | mobile | birthday | home_id |
  682. +----+------------+-----------+----------+------------+---------+
  683. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  684. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  685. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  686. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  687. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  688. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  689. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  690. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  691. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  692. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  693. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  694. +----+------------+-----------+----------+------------+---------+
  695. 11 rows in set (0.00 sec)
  696. mysql> select * from people
  697. -> ;
  698. +----+------------+-----------+----------+------------+---------+
  699. | id | first_name | last_name | mobile | birthday | home_id |
  700. +----+------------+-----------+----------+------------+---------+
  701. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  702. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  703. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  704. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  705. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  706. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  707. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  708. | 13 | Otto | Von Count | 656-6548 | NULL | NULL |
  709. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  710. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  711. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  712. +----+------------+-----------+----------+------------+---------+
  713. 11 rows in set (0.00 sec)
  714. mysql> select * from people where birthday is not null;
  715. +----+------------+-----------+----------+------------+---------+
  716. | id | first_name | last_name | mobile | birthday | home_id |
  717. +----+------------+-----------+----------+------------+---------+
  718. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  719. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  720. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  721. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  722. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  723. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  724. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  725. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  726. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  727. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  728. +----+------------+-----------+----------+------------+---------+
  729. 10 rows in set (0.00 sec)
  730. mysql> SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
  731. +------------+----------+------------+
  732. | first_name | mon | birthday |
  733. +------------+----------+------------+
  734. | Otto | NULL | NULL |
  735. | Tony | January | 1973-01-23 |
  736. | Eli | January | 1984-01-15 |
  737. | Sheeri | February | 1970-02-23 |
  738. | John | April | 1998-04-07 |
  739. | Paul | May | 1996-05-27 |
  740. | Noelle | July | 1960-07-06 |
  741. | Thomas | July | 1987-07-06 |
  742. | Raj | August | 1980-08-31 |
  743. | Jane | December | 1987-12-06 |
  744. | Doug | December | 1954-12-07 |
  745. +------------+----------+------------+
  746. 11 rows in set (0.00 sec)
  747. mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC;
  748. +------------+------------+
  749. | first_name | birthday |
  750. +------------+------------+
  751. | John | 1998-04-07 |
  752. | Paul | 1996-05-27 |
  753. | Jane | 1987-12-06 |
  754. | Thomas | 1987-07-06 |
  755. | Eli | 1984-01-15 |
  756. | Raj | 1980-08-31 |
  757. | Tony | 1973-01-23 |
  758. | Sheeri | 1970-02-23 |
  759. | Noelle | 1960-07-06 |
  760. | Doug | 1954-12-07 |
  761. | Otto | NULL |
  762. +------------+------------+
  763. 11 rows in set (0.00 sec)
  764. mysql> SELECT p.first_name, h.address
  765. -> FROM people p
  766. -> INNER JOIN homes h on (p.home_id = h.id);
  767. +------------+----------------------------------------+
  768. | first_name | address |
  769. +------------+----------------------------------------+
  770. | Noelle | 36 E. Bayberry Rd.Savannah, GA 31404 |
  771. | Thomas | 36 E. Bayberry Rd.Savannah, GA 31404 |
  772. | Paul | 36 E. Bayberry Rd.Savannah, GA 31404 |
  773. | Sheeri | 11 Essex Dr.Farmingdale, NY 11735 |
  774. | Raj | 920 Arlington Street Clifton, NJ 07011 |
  775. | Jane | 920 Arlington Street Clifton, NJ 07011 |
  776. | Doug | 920 Arlington Street Clifton, NJ 07011 |
  777. | John | 234 High Street, PA 19159 |
  778. +------------+----------------------------------------+
  779. 8 rows in set (0.00 sec)
  780. Excercise
  781. mysql> select * from people where birthday is not null;
  782. +----+------------+-----------+----------+------------+---------+
  783. | id | first_name | last_name | mobile | birthday | home_id |
  784. +----+------------+-----------+----------+------------+---------+
  785. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL |
  786. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  787. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  788. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  789. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 |
  790. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  791. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  792. | 14 | John | Smith | NULL | 1998-04-07 | 4 |
  793. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  794. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  795. +----+------------+-----------+----------+------------+---------+
  796. 10 rows in set (0.00 sec)
  797. mysql> SELECT p.first_name, p.last_name,monthname(birthday) as mon,h.address from people p inner join homes h on(p.home_id =h.id)where birthday order by month(birthday);
  798. +------------+-----------+----------+----------------------------------------+
  799. | first_name | last_name | mon | address |
  800. +------------+-----------+----------+----------------------------------------+
  801. | Sheeri | Carbral | February | 11 Essex Dr.Farmingdale, NY 11735 |
  802. | John | Smith | April | 234 High Street, PA 19159 |
  803. | Paul | Thompson | May | 36 E. Bayberry Rd.Savannah, GA 31404 |
  804. | Noelle | Johnson | July | 36 E. Bayberry Rd.Savannah, GA 31404 |
  805. | Thomas | Smith | July | 36 E. Bayberry Rd.Savannah, GA 31404 |
  806. | Raj | Johnson | August | 920 Arlington Street Clifton, NJ 07011 |
  807. | Jane | Smith | December | 920 Arlington Street Clifton, NJ 07011 |
  808. | Doug | Brown | December | 920 Arlington Street Clifton, NJ 07011 |
  809. +------------+-----------+----------+----------------------------------------+
  810. 8 rows in set (0.00 sec)
  811. mysql> select * from people left join homes on (people.home_id =homes.id);
  812. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  813. | id | first_name | last_name | mobile | birthday | home_id | id | address | homenumber |
  814. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  815. | 4 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  816. | 5 | Thomas | Smith | 152-9854 | 1987-07-06 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  817. | 16 | Paul | Thompson | NULL | 1996-05-27 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  818. | 2 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  819. | 3 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  820. | 6 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  821. | 7 | Doug | Brown | 466-6241 | 1954-12-07 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  822. | 14 | John | Smith | NULL | 1998-04-07 | 4 | 4 | 234 High Street, PA 19159 | 267-3940 |
  823. | 1 | Tony | Smith | 152-9854 | 1973-01-23 | NULL | NULL | NULL | NULL |
  824. | 13 | Otto | Von Count | 656-6548 | NULL | NULL | NULL | NULL | NULL |
  825. | 17 | Eli | Kramer | NULL | 1984-01-15 | NULL | NULL | NULL | NULL |
  826. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  827. 11 rows in set (0.00 sec)
  828. Mini Movies
  829. +----+--------------------+---------+-------------+------------+--------+
  830. | id | title | runtime | genre | imdb_score | rating |
  831. +----+--------------------+---------+-------------+------------+--------+
  832. | 1 | Howard the duck | 110 | Sci-Fi | 4.6 | PG |
  833. | 2 | Lavalantula | 83 | Horror | 4.7 | TV-14 |
  834. | 3 | Star Ship Troopers | 129 | Sci-Fi | 7.2 | PG-13 |
  835. | 4 | Waltz With Bashir | 90 | Documentary | 8.0 | R |
  836. | 5 | Spaceballs | 96 | Comedy | 7.1 | PG |
  837. | 6 | Monster Inc. | 92 | Animation | 8.1 | G |
  838. +----+--------------------+---------+-------------+------------+--------+
  839. mysql> select * from movies where genre = 'sci-fi';
  840. +----+--------------------+---------+--------+------------+--------+
  841. | id | title | runtime | genre | imdb_score | rating |
  842. +----+--------------------+---------+--------+------------+--------+
  843. | 1 | Howard the duck | 110 | Sci-Fi | 4.6 | PG |
  844. | 3 | Star Ship Troopers | 129 | Sci-Fi | 7.2 | PG-13 |
  845. +----+--------------------+---------+--------+------------+--------+
  846. mysql> select * from movies where imdb_score >='6.5';
  847. +----+--------------------+---------+-------------+------------+--------+
  848. | id | title | runtime | genre | imdb_score | rating |
  849. +----+--------------------+---------+-------------+------------+--------+
  850. | 3 | Star Ship Troopers | 129 | Sci-Fi | 7.2 | PG-13 |
  851. | 4 | Waltz With Bashir | 90 | Documentary | 8.0 | R |
  852. | 5 | Spaceballs | 96 | Comedy | 7.1 | PG |
  853. | 6 | Monster Inc. | 92 | Animation | 8.1 | G |
  854. +----+--------------------+---------+-------------+------------+--------+
  855. mysql> select title from movies where rating ='PG' or rating = 'G' and runtime < 100;
  856. +-----------------+
  857. | title |
  858. | Spaceballs |
  859. | Monster Inc. |
  860. +-----------------+
  861. mysql> select genre,avg(runtime) from movies where imdb_Score < 7.5 group by genre;
  862. +--------+--------------+
  863. | genre | avg(runtime) |
  864. +--------+--------------+
  865. | Comedy | 96.0000 |
  866. | Horror | 83.0000 |
  867. | Sci-Fi | 119.5000 |
  868. +--------+--------------+
  869. 3 rows in set (0.00 sec)
  870. mysql> update movies set rating ='R' where title = 'Star Ship Troopers';
  871. Query OK, 1 row affected (0.01 sec)
  872. Rows matched: 1 Changed: 1 Warnings: 0
  873. mysql> select * from movies;
  874. +----+--------------------+---------+-------------+------------+--------+
  875. | id | title | runtime | genre | imdb_score | rating |
  876. +----+--------------------+---------+-------------+------------+--------+
  877. | 1 | Howard the duck | 110 | Sci-Fi | 4.6 | PG |
  878. | 2 | Lavalantula | 83 | Horror | 4.7 | TV-14 |
  879. | 3 | Star Ship Troopers | 129 | Sci-Fi | 7.2 | R |
  880. | 4 | Waltz With Bashir | 90 | Documentary | 8.0 | R |
  881. | 5 | Spaceballs | 96 | Comedy | 7.1 | PG |
  882. | 6 | Monster Inc. | 92 | Animation | 8.1 | G |
  883. +----+--------------------+---------+-------------+------------+--------+
  884. 6 rows in set (0.00 sec)
  885. mysql> select id,rating from movies where genre = 'horror' or genre = 'documentary';
  886. +----+--------+
  887. | id | rating |
  888. +----+--------+
  889. | 2 | TV-14 |
  890. | 4 | R |
  891. +----+--------+
  892. 2 rows in set (0.00 sec)
  893. mysql> select max(imdb_score) from movies where rating = 'pg';
  894. +-----------------+
  895. | max(imdb_score) |
  896. +-----------------+
  897. | 7.1 |
  898. +-----------------+
  899. 1 row in set (0.00 sec)
  900. mysql> select max(imdb_score) from movies where rating = 'g';
  901. +-----------------+
  902. | max(imdb_score) |
  903. +-----------------+
  904. | 8.1 |
  905. +-----------------+
  906. mysql> select max(imdb_score) from movies where rating = 'tv-14';
  907. +-----------------+
  908. | max(imdb_score) |
  909. +-----------------+
  910. | 4.7 |
  911. +-----------------+
  912. 1 row in set (0.00 sec)
  913. mysql> select max(imdb_score) from movies where rating = 'r';
  914. +-----------------+
  915. | max(imdb_score) |
  916. +-----------------+
  917. | 8.0 |
  918. +-----------------+
  919. mysql> select min(imdb_score) from movies where rating = 'pg';
  920. +-----------------+
  921. | min(imdb_score) |
  922. +-----------------+
  923. | 4.6 |
  924. +-----------------+
  925. 1 row in set (0.00 sec)
  926. mysql> select min(imdb_score) from movies where rating = 'tv-14';
  927. +-----------------+
  928. | min(imdb_score) |
  929. +-----------------+
  930. | 4.7 |
  931. +-----------------+
  932. 1 row in set (0.00 sec)
  933. mysql> select min(imdb_score) from movies where rating = 'r';
  934. +-----------------+
  935. | min(imdb_score) |
  936. +-----------------+
  937. | 7.2 |
  938. +-----------------+
  939. 1 row in set (0.00 sec)
  940. mysql> select min(imdb_score) from movies where rating = 'g';
  941. +-----------------+
  942. | min(imdb_score) |
  943. +-----------------+
  944. | 8.1 |
  945. +-----------------+
  946. 1 row in set (0.01 sec)
  947. mysql> select avg(imdb_score) from movies where rating = 'pg';
  948. +-----------------+
  949. | avg(imdb_score) |
  950. +-----------------+
  951. | 5.85000 |
  952. +-----------------+
  953. 1 row in set (0.00 sec)
  954. mysql> select avg(imdb_score) from movies where rating = 'tv-14';
  955. +-----------------+
  956. | avg(imdb_score) |
  957. +-----------------+
  958. | 4.70000 |
  959. +-----------------+
  960. 1 row in set (0.00 sec)
  961. mysql> select avg(imdb_score) from movies where rating = 'r';
  962. +-----------------+
  963. | avg(imdb_score) |
  964. +-----------------+
  965. | 7.60000 |
  966. +-----------------+
  967. 1 row in set (0.00 sec)
  968. mysql> select avg(imdb_score) from movies where rating = 'g';
  969. +-----------------+
  970. | avg(imdb_score) |
  971. +-----------------+
  972. | 8.10000 |
  973. +-----------------+
  974. 1 row in set (0.00 sec)
  975. mysql> select rating from movies group by rating having count(*) > 1;;
  976. +--------+
  977. | rating |
  978. +--------+
  979. | PG |
  980. | R |
  981. +--------+
  982. 2 rows in set (0.00 sec)
  983. mysql> delete from movies where rating ='R';
  984. Query OK, 2 rows affected (0.01 sec)
  985. mysql> select * from movies;
  986. +----+-----------------+---------+-----------+------------+--------+
  987. | id | title | runtime | genre | imdb_score | rating |
  988. +----+-----------------+---------+-----------+------------+--------+
  989. | 1 | Howard the duck | 110 | Sci-Fi | 4.6 | PG |
  990. | 2 | Lavalantula | 83 | Horror | 4.7 | TV-14 |
  991. | 5 | Spaceballs | 96 | Comedy | 7.1 | PG |
  992. | 6 | Monster Inc. | 92 | Animation | 8.1 | G |
  993. +----+-----------------+---------+-----------+------------+--------+
  994. 4 rows in set (0.00 sec)