123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. Your MySQL connection id is 14
  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> use data1
  9. Reading table information for completion of table and column names
  10. You can turn off this feature to get a quicker startup with -A
  11. Database changed
  12. mysql> show tables
  13. -> ;
  14. +-----------------+
  15. | Tables_in_data1 |
  16. +-----------------+
  17. | homes |
  18. | movies |
  19. | people |
  20. +-----------------+
  21. 3 rows in set (0.00 sec)
  22. mysql> select * from people;
  23. +----+------------+-----------+----------+------------+---------+
  24. | id | first_name | last_name | mobile | birthday | home_id |
  25. +----+------------+-----------+----------+------------+---------+
  26. | 1 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  27. | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
  28. | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
  29. | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
  30. | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
  31. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  32. +----+------------+-----------+----------+------------+---------+
  33. 6 rows in set (0.00 sec)
  34. mysql> INSERT INTO people (last_name, first_name, mobile, birthday)
  35. -> VALUES ('Smith', 'John', '230-4293', '1973-01-23');
  36. Query OK, 1 row affected (0.00 sec)
  37. mysql> show tables
  38. -> ;
  39. +-----------------+
  40. | Tables_in_data1 |
  41. +-----------------+
  42. | homes |
  43. | movies |
  44. | people |
  45. +-----------------+
  46. 3 rows in set (0.00 sec)
  47. mysql> select * from people;
  48. +----+------------+-----------+----------+------------+---------+
  49. | id | first_name | last_name | mobile | birthday | home_id |
  50. +----+------------+-----------+----------+------------+---------+
  51. | 1 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  52. | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
  53. | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
  54. | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
  55. | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
  56. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  57. | 7 | John | Smith | 230-4293 | 1973-01-23 | NULL |
  58. +----+------------+-----------+----------+------------+---------+
  59. 7 rows in set (0.00 sec)
  60. mysql> UPDATE people SET first_name = 'Tony' WHERE id = 1;
  61. Query OK, 1 row affected (0.01 sec)
  62. Rows matched: 1 Changed: 1 Warnings: 0
  63. mysql> use data
  64. Reading table information for completion of table and column names
  65. You can turn off this feature to get a quicker startup with -A
  66. Database changed
  67. mysql> select * from people;
  68. +----+------------+-----------+----------+------------+---------+
  69. | id | first_name | last_name | mobile | birthday | home_id |
  70. +----+------------+-----------+----------+------------+---------+
  71. | 26 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
  72. | 27 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
  73. | 28 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
  74. | 29 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
  75. | 30 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
  76. +----+------------+-----------+----------+------------+---------+
  77. 5 rows in set (0.00 sec)
  78. mysql> use data1
  79. Reading table information for completion of table and column names
  80. You can turn off this feature to get a quicker startup with -A
  81. Database changed
  82. mysql> select * from people;
  83. +----+------------+-----------+----------+------------+---------+
  84. | id | first_name | last_name | mobile | birthday | home_id |
  85. +----+------------+-----------+----------+------------+---------+
  86. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  87. | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
  88. | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
  89. | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
  90. | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
  91. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  92. | 7 | John | Smith | 230-4293 | 1973-01-23 | NULL |
  93. +----+------------+-----------+----------+------------+---------+
  94. 7 rows in set (0.00 sec)
  95. mysql> UPDATE people SET mobile = '152-9854' WHERE last_name = 'Smith';
  96. Query OK, 3 rows affected (0.01 sec)
  97. Rows matched: 3 Changed: 3 Warnings: 0
  98. mysql> UPDATE people SET birthday = '1955-01-25'
  99. -> WHERE last_name = 'Smith'
  100. -> AND id = 4;
  101. Query OK, 1 row affected (0.01 sec)
  102. Rows matched: 1 Changed: 1 Warnings: 0
  103. mysql> UPDATE people SET mobile = '333-3333', last_name = 'Johnson'
  104. -> WHERE first_name = 'Noelle' OR first_name = 'Raj';
  105. Query OK, 2 rows affected (0.00 sec)
  106. Rows matched: 2 Changed: 2 Warnings: 0
  107. mysql> SELECT COUNT(homenumber) FROM homes;
  108. +-------------------+
  109. | COUNT(homenumber) |
  110. +-------------------+
  111. | 4 |
  112. +-------------------+
  113. 1 row in set (0.01 sec)
  114. mysql> SELECT homenumber FROM homes WHERE id = 1;
  115. +------------+
  116. | homenumber |
  117. +------------+
  118. | 565-6895 |
  119. +------------+
  120. 1 row in set (0.01 sec)
  121. mysql> SELECT COUNT(*) FROM homes;
  122. +----------+
  123. | COUNT(*) |
  124. +----------+
  125. | 4 |
  126. +----------+
  127. 1 row in set (0.00 sec)
  128. mysql> SELECT COUNT(DISTINCT last_name) FROM people;
  129. +---------------------------+
  130. | COUNT(DISTINCT last_name) |
  131. +---------------------------+
  132. | 4 |
  133. +---------------------------+
  134. 1 row in set (0.01 sec)
  135. mysql> select * from people;
  136. +----+------------+-----------+----------+------------+---------+
  137. | id | first_name | last_name | mobile | birthday | home_id |
  138. +----+------------+-----------+----------+------------+---------+
  139. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  140. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  141. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  142. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
  143. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  144. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  145. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
  146. +----+------------+-----------+----------+------------+---------+
  147. 7 rows in set (0.00 sec)
  148. mysql> SELECT SUM(id), AVG(id) FROM people;
  149. +---------+---------+
  150. | SUM(id) | AVG(id) |
  151. +---------+---------+
  152. | 28 | 4.0000 |
  153. +---------+---------+
  154. 1 row in set (0.01 sec)
  155. mysql> select * from homes;
  156. +----+----------------------------------------+------------+
  157. | id | address | homenumber |
  158. +----+----------------------------------------+------------+
  159. | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  160. | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  161. | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  162. | 4 | 234 High Street, PA 19159 | 267-3940 |
  163. +----+----------------------------------------+------------+
  164. 4 rows in set (0.00 sec)
  165. mysql> SELECT SUM(id) AS sum, AVG(id) AS avg FROM people;
  166. +------+--------+
  167. | sum | avg |
  168. +------+--------+
  169. | 28 | 4.0000 |
  170. +------+--------+
  171. 1 row in set (0.05 sec)
  172. mysql> SELECT MIN(birthday) FROM people;
  173. +---------------+
  174. | MIN(birthday) |
  175. +---------------+
  176. | 1954-12-07 |
  177. +---------------+
  178. 1 row in set (0.01 sec)
  179. mysql> SELECT UPPER (first_name), LOWER(last_name) FROM people;
  180. +--------------------+------------------+
  181. | UPPER (first_name) | LOWER(last_name) |
  182. +--------------------+------------------+
  183. | TONY | carbral |
  184. | RAJ | johnson |
  185. | NOELLE | johnson |
  186. | THOMAS | smith |
  187. | JANE | smith |
  188. | DOUG | brown |
  189. | JOHN | smith |
  190. +--------------------+------------------+
  191. 7 rows in set (0.01 sec)
  192. mysql> ELECT REPLACE(last_name, 'a', '1') FROM people;
  193. 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 'ELECT REPLACE(last_name, 'a', '1') FROM people' at line 1
  194. mysql> SELECT REPLACE(last_name, 'a', '1') FROM people;
  195. +------------------------------+
  196. | REPLACE(last_name, 'a', '1') |
  197. +------------------------------+
  198. | C1rbr1l |
  199. | Johnson |
  200. | Johnson |
  201. | Smith |
  202. | Smith |
  203. | Brown |
  204. | Smith |
  205. +------------------------------+
  206. 7 rows in set (0.00 sec)
  207. mysql> SELECT last_name FROM people;
  208. +-----------+
  209. | last_name |
  210. +-----------+
  211. | Carbral |
  212. | Johnson |
  213. | Johnson |
  214. | Smith |
  215. | Smith |
  216. | Brown |
  217. | Smith |
  218. +-----------+
  219. 7 rows in set (0.01 sec)
  220. mysql> INSERT INTO people (first_name, last_name, mobile)
  221. -> VALUES ('Otto', 'Von Count', '656-6548');
  222. Query OK, 1 row affected (0.02 sec)
  223. mysql> select * from people;
  224. +----+------------+-----------+----------+------------+---------+
  225. | id | first_name | last_name | mobile | birthday | home_id |
  226. +----+------------+-----------+----------+------------+---------+
  227. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  228. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  229. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  230. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
  231. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  232. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  233. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
  234. | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
  235. +----+------------+-----------+----------+------------+---------+
  236. 8 rows in set (0.01 sec)
  237. mysql> SELECT CONCAT(first_name, last_name) FROM people
  238. -> WHERE last_name = 'Smith';
  239. +-------------------------------+
  240. | CONCAT(first_name, last_name) |
  241. +-------------------------------+
  242. | ThomasSmith |
  243. | JaneSmith |
  244. | JohnSmith |
  245. +-------------------------------+
  246. 3 rows in set (0.01 sec)
  247. mysql> SELECT CONCAT(first_name, ' ', last_name)
  248. -> FROM people
  249. -> WHERE last_name = 'Smith'
  250. -> ;
  251. +------------------------------------+
  252. | CONCAT(first_name, ' ', last_name) |
  253. +------------------------------------+
  254. | Thomas Smith |
  255. | Jane Smith |
  256. | John Smith |
  257. +------------------------------------+
  258. 3 rows in set (0.00 sec)
  259. mysql> SELECT CONCAT_WS(' ',first_name, last_name, mobile)
  260. -> FROM people WHERE last_name= 'Smith';
  261. +----------------------------------------------+
  262. | CONCAT_WS(' ',first_name, last_name, mobile) |
  263. +----------------------------------------------+
  264. | Thomas Smith 152-9854 |
  265. | Jane Smith 152-9854 |
  266. | John Smith 152-9854 |
  267. +----------------------------------------------+
  268. 3 rows in set (0.00 sec)
  269. mysql> SELECT homenumber, LEFT(homenumber, 3), RIGHT(homenumber, 2) FROM homes;
  270. +------------+---------------------+----------------------+
  271. | homenumber | LEFT(homenumber, 3) | RIGHT(homenumber, 2) |
  272. +------------+---------------------+----------------------+
  273. | 565-6895 | 565 | 95 |
  274. | 454-4544 | 454 | 44 |
  275. | 985-4515 | 985 | 15 |
  276. | 267-3940 | 267 | 40 |
  277. +------------+---------------------+----------------------+
  278. 4 rows in set (0.00 sec)
  279. mysql> SELECT LENGTH(address), CHAR_LENGTH(address) FROM homes;
  280. +-----------------+----------------------+
  281. | LENGTH(address) | CHAR_LENGTH(address) |
  282. +-----------------+----------------------+
  283. | 36 | 36 |
  284. | 33 | 33 |
  285. | 38 | 38 |
  286. | 26 | 26 |
  287. +-----------------+----------------------+
  288. 4 rows in set (0.01 sec)
  289. mysql> SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
  290. +------------+-----------+----------------+
  291. | first_name | last_name | YEAR(birthday) |
  292. +------------+-----------+----------------+
  293. | Raj | Johnson | 1980 |
  294. | John | Smith | 1973 |
  295. +------------+-----------+----------------+
  296. 2 rows in set (0.00 sec)
  297. mysql> SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
  298. +------------+------------+
  299. | first_name | birthday |
  300. +------------+------------+
  301. | Raj | 1980-08-31 |
  302. | Thomas | 1955-01-25 |
  303. +------------+------------+
  304. 2 rows in set (0.00 sec)
  305. mysql> SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
  306. +------------+------------+
  307. | first_name | birthday |
  308. +------------+------------+
  309. | Raj | 1980-08-31 |
  310. | Noelle | 1960-07-06 |
  311. | Thomas | 1955-01-25 |
  312. +------------+------------+
  313. 3 rows in set (0.00 sec)
  314. mysql> SELECT first_name FROM people WHERE RIGHT(first_name,1)='e';
  315. +------------+
  316. | first_name |
  317. +------------+
  318. | Noelle |
  319. | Jane |
  320. +------------+
  321. 2 rows in set (0.00 sec)
  322. mysql> SELECT first_name FROM people WHERE first_name LIKE '%j';
  323. +------------+
  324. | first_name |
  325. +------------+
  326. | Raj |
  327. +------------+
  328. 1 row in set (0.00 sec)
  329. mysql> SELECT first_name FROM people WHERE first_name LIKE '%o%';
  330. +------------+
  331. | first_name |
  332. +------------+
  333. | Tony |
  334. | Noelle |
  335. | Thomas |
  336. | Doug |
  337. | John |
  338. | Otto |
  339. +------------+
  340. 6 rows in set (0.00 sec)
  341. mysql> SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
  342. +------------+
  343. | first_name |
  344. +------------+
  345. | Raj |
  346. | Jane |
  347. +------------+
  348. 2 rows in set (0.00 sec)
  349. mysql> SELECT COUNT(*) FROM people;
  350. +----------+
  351. | COUNT(*) |
  352. +----------+
  353. | 8 |
  354. +----------+
  355. 1 row in set (0.00 sec)
  356. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name
  357. -> ;
  358. +-----------+----------+
  359. | last_name | COUNT(*) |
  360. +-----------+----------+
  361. | Brown | 1 |
  362. | Carbral | 1 |
  363. | Johnson | 2 |
  364. | Smith | 3 |
  365. | Von Count | 1 |
  366. +-----------+----------+
  367. 5 rows in set (0.01 sec)
  368. mysql> SELECT last_name, GROUP_CONCAT(mobile) FROM people GROUP BY last_name;
  369. +-----------+----------------------------+
  370. | last_name | GROUP_CONCAT(mobile) |
  371. +-----------+----------------------------+
  372. | Brown | 466-6241 |
  373. | Carbral | 230-4233 |
  374. | Johnson | 333-3333,333-3333 |
  375. | Smith | 152-9854,152-9854,152-9854 |
  376. | Von Count | 656-6548 |
  377. +-----------+----------------------------+
  378. 5 rows in set (0.00 sec)
  379. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
  380. +-----------+----------------------------------------+
  381. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  382. +-----------+----------------------------------------+
  383. | Brown | 466-6241 |
  384. | Carbral | 230-4233 |
  385. | Johnson | 333-3333 and 333-3333 |
  386. | Smith | 152-9854 and 152-9854 and 152-9854 |
  387. | Von Count | 656-6548 |
  388. +-----------+----------------------------------------+
  389. 5 rows in set (0.00 sec)
  390. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name HAVING COUNT(*)>1;
  391. +-----------+----------------------------------------+
  392. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  393. +-----------+----------------------------------------+
  394. | Johnson | 333-3333 and 333-3333 |
  395. | Smith | 152-9854 and 152-9854 and 152-9854 |
  396. +-----------+----------------------------------------+
  397. 2 rows in set (0.00 sec)
  398. mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name HAVING COUNT(*)>1;
  399. +-----------+----------------------------------------+
  400. | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
  401. +-----------+----------------------------------------+
  402. | Johnson | 333-3333 and 333-3333 |
  403. | Smith | 152-9854 and 152-9854 and 152-9854 |
  404. +-----------+----------------------------------------+
  405. 2 rows in set (0.00 sec)
  406. mysql> SELECT first_name, birthday FROM people ORDER BY birthday;
  407. +------------+------------+
  408. | first_name | birthday |
  409. +------------+------------+
  410. | Otto | NULL |
  411. | Doug | 1954-12-07 |
  412. | Thomas | 1955-01-25 |
  413. | Noelle | 1960-07-06 |
  414. | Tony | 1970-02-23 |
  415. | John | 1973-01-23 |
  416. | Raj | 1980-08-31 |
  417. | Jane | 1987-12-06 |
  418. +------------+------------+
  419. 8 rows in set (0.00 sec)
  420. mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC;
  421. +------------+------------+
  422. | first_name | birthday |
  423. +------------+------------+
  424. | Jane | 1987-12-06 |
  425. | Raj | 1980-08-31 |
  426. | John | 1973-01-23 |
  427. | Tony | 1970-02-23 |
  428. | Noelle | 1960-07-06 |
  429. | Thomas | 1955-01-25 |
  430. | Doug | 1954-12-07 |
  431. | Otto | NULL |
  432. +------------+------------+
  433. 8 rows in set (0.00 sec)
  434. mysql>
  435. mysql> SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
  436. +------------+-----------+
  437. | first_name | last_name |
  438. +------------+-----------+
  439. | Doug | Brown |
  440. | Tony | Carbral |
  441. | Noelle | Johnson |
  442. | Raj | Johnson |
  443. | Jane | Smith |
  444. | John | Smith |
  445. | Thomas | Smith |
  446. | Otto | Von Count |
  447. +------------+-----------+
  448. 8 rows in set (0.00 sec)
  449. mysql>
  450. mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
  451. +------------+------------+
  452. | first_name | birthday |
  453. +------------+------------+
  454. | Jane | 1987-12-06 |
  455. | Raj | 1980-08-31 |
  456. | John | 1973-01-23 |
  457. +------------+------------+
  458. 3 rows in set (0.00 sec)
  459. mysql> SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
  460. +------------+----------+------------+
  461. | first_name | mon | birthday |
  462. +------------+----------+------------+
  463. | Otto | NULL | NULL |
  464. | Thomas | January | 1955-01-25 |
  465. | John | January | 1973-01-23 |
  466. | Tony | February | 1970-02-23 |
  467. | Noelle | July | 1960-07-06 |
  468. | Raj | August | 1980-08-31 |
  469. | Jane | December | 1987-12-06 |
  470. | Doug | December | 1954-12-07 |
  471. +------------+----------+------------+
  472. 8 rows in set (0.00 sec)
  473. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
  474. +-----------+----------+
  475. | last_name | COUNT(*) |
  476. +-----------+----------+
  477. | Brown | 1 |
  478. | Carbral | 1 |
  479. | Johnson | 2 |
  480. | Smith | 3 |
  481. | Von Count | 1 |
  482. +-----------+----------+
  483. 5 rows in set (0.00 sec)
  484. mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name ORDER BY NULL;
  485. +-----------+----------+
  486. | last_name | COUNT(*) |
  487. +-----------+----------+
  488. | Carbral | 1 |
  489. | Johnson | 2 |
  490. | Smith | 3 |
  491. | Brown | 1 |
  492. | Von Count | 1 |
  493. +-----------+----------+
  494. 5 rows in set (0.00 sec)
  495. mysql> INSERT INTO people (first_name, last_name, birthday, home_id)
  496. -> VALUES ('John', 'Smith', '1998-04-07', 4),
  497. -> ('Maya', 'Wasserman' , NULL, 4),
  498. -> ('Paul', 'Thompson', '1996-05-27', 1)
  499. -> ;
  500. Query OK, 3 rows affected (0.01 sec)
  501. Records: 3 Duplicates: 0 Warnings: 0
  502. mysql> DELETE FROM people WHERE first_name='Maya';
  503. Query OK, 1 row affected (0.01 sec)
  504. mysql> select * from people;
  505. +----+------------+-----------+----------+------------+---------+
  506. | id | first_name | last_name | mobile | birthday | home_id |
  507. +----+------------+-----------+----------+------------+---------+
  508. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  509. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  510. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  511. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
  512. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  513. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  514. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
  515. | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
  516. | 9 | John | Smith | NULL | 1998-04-07 | 4 |
  517. | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  518. +----+------------+-----------+----------+------------+---------+
  519. 10 rows in set (0.00 sec)
  520. mysql> INSERT INTO people (first_name, last_name, birthday)
  521. -> VALUES ('Eli', 'Kramer', '1984-01-15');
  522. Query OK, 1 row affected (0.01 sec)
  523. mysql> select * from people;
  524. +----+------------+-----------+----------+------------+---------+
  525. | id | first_name | last_name | mobile | birthday | home_id |
  526. +----+------------+-----------+----------+------------+---------+
  527. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  528. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  529. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  530. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
  531. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  532. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  533. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
  534. | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
  535. | 9 | John | Smith | NULL | 1998-04-07 | 4 |
  536. | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  537. | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  538. +----+------------+-----------+----------+------------+---------+
  539. 11 rows in set (0.00 sec)
  540. mysql> select * from homes;
  541. +----+----------------------------------------+------------+
  542. | id | address | homenumber |
  543. +----+----------------------------------------+------------+
  544. | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  545. | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  546. | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  547. | 4 | 234 High Street, PA 19159 | 267-3940 |
  548. +----+----------------------------------------+------------+
  549. 4 rows in set (0.00 sec)
  550. mysql> SELECT p.first_name, h.address
  551. -> FROM people p
  552. -> INNER JOIN homes h on (p.home_id = h.id);
  553. +------------+----------------------------------------+
  554. | first_name | address |
  555. +------------+----------------------------------------+
  556. | Noelle | 36 E. Bayberry Rd.Savannah, GA 31404 |
  557. | Thomas | 36 E. Bayberry Rd.Savannah, GA 31404 |
  558. | Paul | 36 E. Bayberry Rd.Savannah, GA 31404 |
  559. | Tony | 11 Essex Dr.Farmingdale, NY 11735 |
  560. | Raj | 920 Arlington Street Clifton, NJ 07011 |
  561. | Jane | 920 Arlington Street Clifton, NJ 07011 |
  562. | Doug | 920 Arlington Street Clifton, NJ 07011 |
  563. | John | 234 High Street, PA 19159 |
  564. +------------+----------------------------------------+
  565. 8 rows in set (0.01 sec)
  566. mysql> SELECT first_name, last_name
  567. -> FROM people p
  568. -> INNER JOIN homes h on (p.home_id = h.id)
  569. -> WHERE p.HOME_ID = 1;
  570. +------------+-----------+
  571. | first_name | last_name |
  572. +------------+-----------+
  573. | Noelle | Johnson |
  574. | Thomas | Smith |
  575. | Paul | Thompson |
  576. +------------+-----------+
  577. 3 rows in set (0.00 sec)
  578. mysql> SELECT p.*, h.address, h.homenumber
  579. -> FROM people p
  580. -> INNER JOIN homes h on (p.home_id = h.id)
  581. -> WHERE p.first_name LIKE '%e%';
  582. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  583. | id | first_name | last_name | mobile | birthday | home_id | address | homenumber |
  584. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  585. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  586. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  587. +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
  588. 2 rows in set (0.00 sec)
  589. Exercise//////////////////////////////////////////////////////////////
  590. mysql> SELECT * FROM PEOPLE WHERE BIRTHDAY IS NOT NULL;
  591. +----+------------+-----------+----------+------------+---------+
  592. | id | first_name | last_name | mobile | birthday | home_id |
  593. +----+------------+-----------+----------+------------+---------+
  594. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
  595. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
  596. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
  597. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
  598. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
  599. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
  600. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
  601. | 9 | John | Smith | NULL | 1998-04-07 | 4 |
  602. | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
  603. | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL |
  604. +----+------------+-----------+----------+------------+---------+
  605. 10 rows in set (0.00 sec)
  606. 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);
  607. +------------+-----------+----------+----------------------------------------+
  608. | first_name | last_name | mon | address |
  609. +------------+-----------+----------+----------------------------------------+
  610. | Thomas | Smith | January | 36 E. Bayberry Rd.Savannah, GA 31404 |
  611. | Tony | Carbral | February | 11 Essex Dr.Farmingdale, NY 11735 |
  612. | John | Smith | April | 234 High Street, PA 19159 |
  613. | Paul | Thompson | May | 36 E. Bayberry Rd.Savannah, GA 31404 |
  614. | Noelle | Johnson | July | 36 E. Bayberry Rd.Savannah, GA 31404 |
  615. | Raj | Johnson | August | 920 Arlington Street Clifton, NJ 07011 |
  616. | Jane | Smith | December | 920 Arlington Street Clifton, NJ 07011 |
  617. | Doug | Brown | December | 920 Arlington Street Clifton, NJ 07011 |
  618. +------------+-----------+----------+----------------------------------------+
  619. 8 rows in set (0.00 sec)
  620. mysql> select * from people left join homes on (people.home_id = homes.id);
  621. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  622. | id | first_name | last_name | mobile | birthday | home_id | id | address | homenumber |
  623. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  624. | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  625. | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  626. | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
  627. | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
  628. | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  629. | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  630. | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
  631. | 9 | John | Smith | NULL | 1998-04-07 | 4 | 4 | 234 High Street, PA 19159 | 267-3940 |
  632. | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL | NULL | NULL | NULL |
  633. | 8 | Otto | Von Count | 656-6548 | NULL | NULL | NULL | NULL | NULL |
  634. | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL | NULL | NULL | NULL |
  635. +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
  636. 11 rows in set (0.00 sec)
  637. movies Exercise////////////////////////////////
  638. mysql> select * from movies
  639. -> ;
  640. +----+-------------------+---------+-------------+------------+--------+
  641. | id | title | runtime | genre | imdb_score | rating |
  642. +----+-------------------+---------+-------------+------------+--------+
  643. | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
  644. | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
  645. | 3 | starship_troopers | 129 | sci-fi | 7.2 | pg-13 |
  646. | 4 | waltz_with_bashir | 90 | documentary | 8.0 | r |
  647. | 5 | spaceballs | 96 | comedy | 7.1 | pg |
  648. | 6 | monsters_inc. | 92 | animation | 8.1 | g |
  649. +----+-------------------+---------+-------------+------------+--------+
  650. 6 rows in set (0.00 sec)
  651. mysql> select * from movies where genre = 'sci-fi';
  652. +----+-------------------+---------+--------+------------+--------+
  653. | id | title | runtime | genre | imdb_score | rating |
  654. +----+-------------------+---------+--------+------------+--------+
  655. | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
  656. | 3 | starship_troopers | 129 | sci-fi | 7.2 | pg-13 |
  657. +----+-------------------+---------+--------+------------+--------+
  658. 2 rows in set (0.00 sec)
  659. mysql> select imdb_score from movies where imdb_score >= '6.5';
  660. +------------+
  661. | imdb_score |
  662. +------------+
  663. | 7.2 |
  664. | 8.0 |
  665. | 7.1 |
  666. | 8.1 |
  667. +------------+
  668. 4 rows in set (0.00 sec)
  669. mysql> select title from movies where rating = 'g' or rating = 'pg' and runtime <100;
  670. +---------------+
  671. | title |
  672. +---------------+
  673. | spaceballs |
  674. | monsters_inc. |
  675. +---------------+
  676. 2 rows in set (0.00 sec)
  677. mysql> select genre, avg(runtime) FROM movies where imdb_score < 7.5 group by genre;
  678. +--------+--------------+
  679. | genre | avg(runtime) |
  680. +--------+--------------+
  681. | comedy | 96.0000 |
  682. | horror | 83.0000 |
  683. | sci-fi | 119.5000 |
  684. +--------+--------------+
  685. 3 rows in set (0.00 sec)
  686. mysql> select*from movies;
  687. +----+-------------------+---------+-------------+------------+--------+
  688. | id | title | runtime | genre | imdb_score | rating |
  689. +----+-------------------+---------+-------------+------------+--------+
  690. | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
  691. | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
  692. | 3 | starship_troopers | 129 | sci-fi | 7.2 | r |
  693. | 4 | waltz_with_bashir | 90 | documentary | 8.0 | r |
  694. | 5 | spaceballs | 96 | comedy | 7.1 | pg |
  695. | 6 | monsters_inc. | 92 | animation | 8.1 | g |
  696. +----+-------------------+---------+-------------+------------+--------+
  697. 6 rows in set (0.00 sec)
  698. mysql> select id, rating from movies where genre = 'horror' or genre = 'documentary';
  699. +----+--------+
  700. | id | rating |
  701. +----+--------+
  702. | 2 | tv-14 |
  703. | 4 | r |
  704. +----+--------+
  705. 2 rows in set (0.00 sec)
  706. mysql> select max(imdb_score) from movies where rating = 'pg' ;
  707. +-----------------+
  708. | max(imdb_score) |
  709. +-----------------+
  710. | 7.1 |
  711. +-----------------+
  712. 1 row in set (0.00 sec)
  713. mysql> select avg(imdb_score) from movies where rating = 'pg' ;
  714. +-----------------+
  715. | avg(imdb_score) |
  716. +-----------------+
  717. | 5.85000 |
  718. +-----------------+
  719. 1 row in set (0.00 sec)
  720. mysql> select min(imdb_score) from movies where rating = 'pg' ;
  721. +-----------------+
  722. | min(imdb_score) |
  723. +-----------------+
  724. | 4.6 |
  725. +-----------------+
  726. 1 row in set (0.00 sec)
  727. mysql> select min(imdb_score) from movies where rating = 'g' ;
  728. +-----------------+
  729. | min(imdb_score) |
  730. +-----------------+
  731. | 8.1 |
  732. +-----------------+
  733. 1 row in set (0.00 sec)
  734. mysql> select max(imdb_score) from movies where rating = 'g' ;
  735. +-----------------+
  736. | max(imdb_score) |
  737. +-----------------+
  738. | 8.1 |
  739. +-----------------+
  740. 1 row in set (0.00 sec)
  741. mysql> select avg(imdb_score) from movies where rating = 'g' ;
  742. +-----------------+
  743. | avg(imdb_score) |
  744. +-----------------+
  745. | 8.10000 |
  746. +-----------------+
  747. 1 row in set (0.00 sec)
  748. mysql> select avg(imdb_score) from movies where rating = 'r' ;
  749. +-----------------+
  750. | avg(imdb_score) |
  751. +-----------------+
  752. | 7.60000 |
  753. +-----------------+
  754. 1 row in set (0.01 sec)
  755. mysql> select min(imdb_score) from movies where rating = 'r' ;
  756. +-----------------+
  757. | min(imdb_score) |
  758. +-----------------+
  759. | 7.2 |
  760. +-----------------+
  761. 1 row in set (0.00 sec)
  762. mysql> select max(imdb_score) from movies where rating = 'r' ;
  763. +-----------------+
  764. | max(imdb_score) |
  765. +-----------------+
  766. | 8.0 |
  767. +-----------------+
  768. 1 row in set (0.00 sec)
  769. mysql> select max(imdb_score) from movies where rating = 'tv-14' ;
  770. +-----------------+
  771. | max(imdb_score) |
  772. +-----------------+
  773. | 4.7 |
  774. +-----------------+
  775. 1 row in set (0.00 sec)
  776. mysql> select min(imdb_score) from movies where rating = 'tv-14' ;
  777. +-----------------+
  778. | min(imdb_score) |
  779. +-----------------+
  780. | 4.7 |
  781. +-----------------+
  782. 1 row in set (0.00 sec)
  783. mysql> select avg(imdb_score) from movies where rating = 'tv-14' ;
  784. +-----------------+
  785. | avg(imdb_score) |
  786. +-----------------+
  787. | 4.7 |
  788. +-----------------+
  789. 1 row in set (0.00 sec)
  790. mysql> select rating from movies group by rating having count(*)>1;
  791. +--------+
  792. | rating |
  793. +--------+
  794. | pg |
  795. | r |
  796. +--------+
  797. 2 rows in set (0.00 sec)
  798. mysql> delete from movies where rating = 'r';
  799. Query OK, 2 rows affected (0.01 sec)
  800. mysql> select * from movies;
  801. +----+-----------------+---------+-----------+------------+--------+
  802. | id | title | runtime | genre | imdb_score | rating |
  803. +----+-----------------+---------+-----------+------------+--------+
  804. | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
  805. | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
  806. | 5 | spaceballs | 96 | comedy | 7.1 | pg |
  807. | 6 | monsters_inc. | 92 | animation | 8.1 | g |
  808. +----+-----------------+---------+-----------+------------+--------+
  809. 4 rows in set (0.00 sec)