123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905 |
-
-
- Your MySQL connection id is 14
- Server version: 5.7.22 Homebrew
-
- Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> use data1
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> show tables
- -> ;
- +-----------------+
- | Tables_in_data1 |
- +-----------------+
- | homes |
- | movies |
- | people |
- +-----------------+
- 3 rows in set (0.00 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
- | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
- | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- +----+------------+-----------+----------+------------+---------+
- 6 rows in set (0.00 sec)
-
- mysql> INSERT INTO people (last_name, first_name, mobile, birthday)
- -> VALUES ('Smith', 'John', '230-4293', '1973-01-23');
- Query OK, 1 row affected (0.00 sec)
-
- mysql> show tables
- -> ;
- +-----------------+
- | Tables_in_data1 |
- +-----------------+
- | homes |
- | movies |
- | people |
- +-----------------+
- 3 rows in set (0.00 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
- | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
- | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 230-4293 | 1973-01-23 | NULL |
- +----+------------+-----------+----------+------------+---------+
- 7 rows in set (0.00 sec)
-
- mysql> UPDATE people SET first_name = 'Tony' WHERE id = 1;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
-
- mysql> use data
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 26 | Sheeri | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 27 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
- | 28 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
- | 29 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
- | 30 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
- +----+------------+-----------+----------+------------+---------+
- 5 rows in set (0.00 sec)
-
- mysql> use data1
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Sharam | 186-5223 | 1980-08-31 | 3 |
- | 3 | Noelle | Durand | 395-6161 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 395-6181 | 1987-07-06 | 1 |
- | 5 | Jane | Smith | 393-6181 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 230-4293 | 1973-01-23 | NULL |
- +----+------------+-----------+----------+------------+---------+
- 7 rows in set (0.00 sec)
-
- mysql> UPDATE people SET mobile = '152-9854' WHERE last_name = 'Smith';
- Query OK, 3 rows affected (0.01 sec)
- Rows matched: 3 Changed: 3 Warnings: 0
-
- mysql> UPDATE people SET birthday = '1955-01-25'
- -> WHERE last_name = 'Smith'
- -> AND id = 4;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
-
- mysql> UPDATE people SET mobile = '333-3333', last_name = 'Johnson'
- -> WHERE first_name = 'Noelle' OR first_name = 'Raj';
- Query OK, 2 rows affected (0.00 sec)
- Rows matched: 2 Changed: 2 Warnings: 0
-
- mysql> SELECT COUNT(homenumber) FROM homes;
- +-------------------+
- | COUNT(homenumber) |
- +-------------------+
- | 4 |
- +-------------------+
- 1 row in set (0.01 sec)
-
- mysql> SELECT homenumber FROM homes WHERE id = 1;
- +------------+
- | homenumber |
- +------------+
- | 565-6895 |
- +------------+
- 1 row in set (0.01 sec)
-
- mysql> SELECT COUNT(*) FROM homes;
- +----------+
- | COUNT(*) |
- +----------+
- | 4 |
- +----------+
- 1 row in set (0.00 sec)
-
- mysql> SELECT COUNT(DISTINCT last_name) FROM people;
- +---------------------------+
- | COUNT(DISTINCT last_name) |
- +---------------------------+
- | 4 |
- +---------------------------+
- 1 row in set (0.01 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
- +----+------------+-----------+----------+------------+---------+
- 7 rows in set (0.00 sec)
-
- mysql> SELECT SUM(id), AVG(id) FROM people;
- +---------+---------+
- | SUM(id) | AVG(id) |
- +---------+---------+
- | 28 | 4.0000 |
- +---------+---------+
- 1 row in set (0.01 sec)
-
- mysql> select * from homes;
- +----+----------------------------------------+------------+
- | id | address | homenumber |
- +----+----------------------------------------+------------+
- | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
- | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- | 4 | 234 High Street, PA 19159 | 267-3940 |
- +----+----------------------------------------+------------+
- 4 rows in set (0.00 sec)
-
- mysql> SELECT SUM(id) AS sum, AVG(id) AS avg FROM people;
- +------+--------+
- | sum | avg |
- +------+--------+
- | 28 | 4.0000 |
- +------+--------+
- 1 row in set (0.05 sec)
-
- mysql> SELECT MIN(birthday) FROM people;
- +---------------+
- | MIN(birthday) |
- +---------------+
- | 1954-12-07 |
- +---------------+
- 1 row in set (0.01 sec)
-
- mysql> SELECT UPPER (first_name), LOWER(last_name) FROM people;
- +--------------------+------------------+
- | UPPER (first_name) | LOWER(last_name) |
- +--------------------+------------------+
- | TONY | carbral |
- | RAJ | johnson |
- | NOELLE | johnson |
- | THOMAS | smith |
- | JANE | smith |
- | DOUG | brown |
- | JOHN | smith |
- +--------------------+------------------+
- 7 rows in set (0.01 sec)
-
- mysql> ELECT REPLACE(last_name, 'a', '1') FROM people;
- 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
- mysql> SELECT REPLACE(last_name, 'a', '1') FROM people;
- +------------------------------+
- | REPLACE(last_name, 'a', '1') |
- +------------------------------+
- | C1rbr1l |
- | Johnson |
- | Johnson |
- | Smith |
- | Smith |
- | Brown |
- | Smith |
- +------------------------------+
- 7 rows in set (0.00 sec)
-
- mysql> SELECT last_name FROM people;
- +-----------+
- | last_name |
- +-----------+
- | Carbral |
- | Johnson |
- | Johnson |
- | Smith |
- | Smith |
- | Brown |
- | Smith |
- +-----------+
- 7 rows in set (0.01 sec)
-
- mysql> INSERT INTO people (first_name, last_name, mobile)
- -> VALUES ('Otto', 'Von Count', '656-6548');
- Query OK, 1 row affected (0.02 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
- | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
- +----+------------+-----------+----------+------------+---------+
- 8 rows in set (0.01 sec)
-
- mysql> SELECT CONCAT(first_name, last_name) FROM people
- -> WHERE last_name = 'Smith';
- +-------------------------------+
- | CONCAT(first_name, last_name) |
- +-------------------------------+
- | ThomasSmith |
- | JaneSmith |
- | JohnSmith |
- +-------------------------------+
- 3 rows in set (0.01 sec)
-
- mysql> SELECT CONCAT(first_name, ' ', last_name)
- -> FROM people
- -> WHERE last_name = 'Smith'
- -> ;
- +------------------------------------+
- | CONCAT(first_name, ' ', last_name) |
- +------------------------------------+
- | Thomas Smith |
- | Jane Smith |
- | John Smith |
- +------------------------------------+
- 3 rows in set (0.00 sec)
-
- mysql> SELECT CONCAT_WS(' ',first_name, last_name, mobile)
- -> FROM people WHERE last_name= 'Smith';
- +----------------------------------------------+
- | CONCAT_WS(' ',first_name, last_name, mobile) |
- +----------------------------------------------+
- | Thomas Smith 152-9854 |
- | Jane Smith 152-9854 |
- | John Smith 152-9854 |
- +----------------------------------------------+
- 3 rows in set (0.00 sec)
-
- mysql> SELECT homenumber, LEFT(homenumber, 3), RIGHT(homenumber, 2) FROM homes;
- +------------+---------------------+----------------------+
- | homenumber | LEFT(homenumber, 3) | RIGHT(homenumber, 2) |
- +------------+---------------------+----------------------+
- | 565-6895 | 565 | 95 |
- | 454-4544 | 454 | 44 |
- | 985-4515 | 985 | 15 |
- | 267-3940 | 267 | 40 |
- +------------+---------------------+----------------------+
- 4 rows in set (0.00 sec)
-
- mysql> SELECT LENGTH(address), CHAR_LENGTH(address) FROM homes;
- +-----------------+----------------------+
- | LENGTH(address) | CHAR_LENGTH(address) |
- +-----------------+----------------------+
- | 36 | 36 |
- | 33 | 33 |
- | 38 | 38 |
- | 26 | 26 |
- +-----------------+----------------------+
- 4 rows in set (0.01 sec)
-
- mysql> SELECT first_name, last_name, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
- +------------+-----------+----------------+
- | first_name | last_name | YEAR(birthday) |
- +------------+-----------+----------------+
- | Raj | Johnson | 1980 |
- | John | Smith | 1973 |
- +------------+-----------+----------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT first_name, birthday FROM people WHERE first_name='Thomas' OR first_name='Raj' OR first_name='Sheeri';
- +------------+------------+
- | first_name | birthday |
- +------------+------------+
- | Raj | 1980-08-31 |
- | Thomas | 1955-01-25 |
- +------------+------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT first_name, birthday FROM people WHERE first_name IN ('Noelle', 'Thomas', 'Raj');
- +------------+------------+
- | first_name | birthday |
- +------------+------------+
- | Raj | 1980-08-31 |
- | Noelle | 1960-07-06 |
- | Thomas | 1955-01-25 |
- +------------+------------+
- 3 rows in set (0.00 sec)
-
- mysql> SELECT first_name FROM people WHERE RIGHT(first_name,1)='e';
- +------------+
- | first_name |
- +------------+
- | Noelle |
- | Jane |
- +------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT first_name FROM people WHERE first_name LIKE '%j';
- +------------+
- | first_name |
- +------------+
- | Raj |
- +------------+
- 1 row in set (0.00 sec)
-
- mysql> SELECT first_name FROM people WHERE first_name LIKE '%o%';
- +------------+
- | first_name |
- +------------+
- | Tony |
- | Noelle |
- | Thomas |
- | Doug |
- | John |
- | Otto |
- +------------+
- 6 rows in set (0.00 sec)
-
- mysql> SELECT first_name FROM people WHERE first_name NOT LIKE '%o%';
- +------------+
- | first_name |
- +------------+
- | Raj |
- | Jane |
- +------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT COUNT(*) FROM people;
- +----------+
- | COUNT(*) |
- +----------+
- | 8 |
- +----------+
- 1 row in set (0.00 sec)
-
- mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name
- -> ;
- +-----------+----------+
- | last_name | COUNT(*) |
- +-----------+----------+
- | Brown | 1 |
- | Carbral | 1 |
- | Johnson | 2 |
- | Smith | 3 |
- | Von Count | 1 |
- +-----------+----------+
- 5 rows in set (0.01 sec)
-
- mysql> SELECT last_name, GROUP_CONCAT(mobile) FROM people GROUP BY last_name;
- +-----------+----------------------------+
- | last_name | GROUP_CONCAT(mobile) |
- +-----------+----------------------------+
- | Brown | 466-6241 |
- | Carbral | 230-4233 |
- | Johnson | 333-3333,333-3333 |
- | Smith | 152-9854,152-9854,152-9854 |
- | Von Count | 656-6548 |
- +-----------+----------------------------+
- 5 rows in set (0.00 sec)
-
- mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name;
- +-----------+----------------------------------------+
- | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
- +-----------+----------------------------------------+
- | Brown | 466-6241 |
- | Carbral | 230-4233 |
- | Johnson | 333-3333 and 333-3333 |
- | Smith | 152-9854 and 152-9854 and 152-9854 |
- | Von Count | 656-6548 |
- +-----------+----------------------------------------+
- 5 rows in set (0.00 sec)
-
- mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY last_name HAVING COUNT(*)>1;
- +-----------+----------------------------------------+
- | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
- +-----------+----------------------------------------+
- | Johnson | 333-3333 and 333-3333 |
- | Smith | 152-9854 and 152-9854 and 152-9854 |
- +-----------+----------------------------------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT last_name, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE last_name != 'Cabral' GROUP BY last_name HAVING COUNT(*)>1;
- +-----------+----------------------------------------+
- | last_name | GROUP_CONCAT(mobile SEPARATOR ' and ') |
- +-----------+----------------------------------------+
- | Johnson | 333-3333 and 333-3333 |
- | Smith | 152-9854 and 152-9854 and 152-9854 |
- +-----------+----------------------------------------+
- 2 rows in set (0.00 sec)
-
- mysql> SELECT first_name, birthday FROM people ORDER BY birthday;
- +------------+------------+
- | first_name | birthday |
- +------------+------------+
- | Otto | NULL |
- | Doug | 1954-12-07 |
- | Thomas | 1955-01-25 |
- | Noelle | 1960-07-06 |
- | Tony | 1970-02-23 |
- | John | 1973-01-23 |
- | Raj | 1980-08-31 |
- | Jane | 1987-12-06 |
- +------------+------------+
- 8 rows in set (0.00 sec)
-
- mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC;
- +------------+------------+
- | first_name | birthday |
- +------------+------------+
- | Jane | 1987-12-06 |
- | Raj | 1980-08-31 |
- | John | 1973-01-23 |
- | Tony | 1970-02-23 |
- | Noelle | 1960-07-06 |
- | Thomas | 1955-01-25 |
- | Doug | 1954-12-07 |
- | Otto | NULL |
- +------------+------------+
- 8 rows in set (0.00 sec)
-
- mysql>
- mysql> SELECT first_name, last_name FROM people ORDER BY last_name, first_name;
- +------------+-----------+
- | first_name | last_name |
- +------------+-----------+
- | Doug | Brown |
- | Tony | Carbral |
- | Noelle | Johnson |
- | Raj | Johnson |
- | Jane | Smith |
- | John | Smith |
- | Thomas | Smith |
- | Otto | Von Count |
- +------------+-----------+
- 8 rows in set (0.00 sec)
-
- mysql>
- mysql> SELECT first_name, birthday FROM people ORDER BY birthday DESC LIMIT 3;
- +------------+------------+
- | first_name | birthday |
- +------------+------------+
- | Jane | 1987-12-06 |
- | Raj | 1980-08-31 |
- | John | 1973-01-23 |
- +------------+------------+
- 3 rows in set (0.00 sec)
-
- mysql> SELECT first_name, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
- +------------+----------+------------+
- | first_name | mon | birthday |
- +------------+----------+------------+
- | Otto | NULL | NULL |
- | Thomas | January | 1955-01-25 |
- | John | January | 1973-01-23 |
- | Tony | February | 1970-02-23 |
- | Noelle | July | 1960-07-06 |
- | Raj | August | 1980-08-31 |
- | Jane | December | 1987-12-06 |
- | Doug | December | 1954-12-07 |
- +------------+----------+------------+
- 8 rows in set (0.00 sec)
-
- mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name;
- +-----------+----------+
- | last_name | COUNT(*) |
- +-----------+----------+
- | Brown | 1 |
- | Carbral | 1 |
- | Johnson | 2 |
- | Smith | 3 |
- | Von Count | 1 |
- +-----------+----------+
- 5 rows in set (0.00 sec)
-
- mysql> SELECT last_name, COUNT(*) FROM people GROUP BY last_name ORDER BY NULL;
- +-----------+----------+
- | last_name | COUNT(*) |
- +-----------+----------+
- | Carbral | 1 |
- | Johnson | 2 |
- | Smith | 3 |
- | Brown | 1 |
- | Von Count | 1 |
- +-----------+----------+
- 5 rows in set (0.00 sec)
-
- mysql> INSERT INTO people (first_name, last_name, birthday, home_id)
- -> VALUES ('John', 'Smith', '1998-04-07', 4),
- -> ('Maya', 'Wasserman' , NULL, 4),
- -> ('Paul', 'Thompson', '1996-05-27', 1)
- -> ;
- Query OK, 3 rows affected (0.01 sec)
- Records: 3 Duplicates: 0 Warnings: 0
-
- mysql> DELETE FROM people WHERE first_name='Maya';
- Query OK, 1 row affected (0.01 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
- | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
- | 9 | John | Smith | NULL | 1998-04-07 | 4 |
- | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
- +----+------------+-----------+----------+------------+---------+
- 10 rows in set (0.00 sec)
-
- mysql> INSERT INTO people (first_name, last_name, birthday)
- -> VALUES ('Eli', 'Kramer', '1984-01-15');
- Query OK, 1 row affected (0.01 sec)
-
- mysql> select * from people;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
- | 8 | Otto | Von Count | 656-6548 | NULL | NULL |
- | 9 | John | Smith | NULL | 1998-04-07 | 4 |
- | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
- | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL |
- +----+------------+-----------+----------+------------+---------+
- 11 rows in set (0.00 sec)
-
- mysql> select * from homes;
- +----+----------------------------------------+------------+
- | id | address | homenumber |
- +----+----------------------------------------+------------+
- | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
- | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- | 4 | 234 High Street, PA 19159 | 267-3940 |
- +----+----------------------------------------+------------+
- 4 rows in set (0.00 sec)
-
- mysql> SELECT p.first_name, h.address
- -> FROM people p
- -> INNER JOIN homes h on (p.home_id = h.id);
- +------------+----------------------------------------+
- | first_name | address |
- +------------+----------------------------------------+
- | Noelle | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Thomas | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Paul | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Tony | 11 Essex Dr.Farmingdale, NY 11735 |
- | Raj | 920 Arlington Street Clifton, NJ 07011 |
- | Jane | 920 Arlington Street Clifton, NJ 07011 |
- | Doug | 920 Arlington Street Clifton, NJ 07011 |
- | John | 234 High Street, PA 19159 |
- +------------+----------------------------------------+
- 8 rows in set (0.01 sec)
-
- mysql> SELECT first_name, last_name
- -> FROM people p
- -> INNER JOIN homes h on (p.home_id = h.id)
- -> WHERE p.HOME_ID = 1;
- +------------+-----------+
- | first_name | last_name |
- +------------+-----------+
- | Noelle | Johnson |
- | Thomas | Smith |
- | Paul | Thompson |
- +------------+-----------+
- 3 rows in set (0.00 sec)
-
- mysql> SELECT p.*, h.address, h.homenumber
- -> FROM people p
- -> INNER JOIN homes h on (p.home_id = h.id)
- -> WHERE p.first_name LIKE '%e%';
- +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
- | id | first_name | last_name | mobile | birthday | home_id | address | homenumber |
- +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- +----+------------+-----------+----------+------------+---------+----------------------------------------+------------+
- 2 rows in set (0.00 sec)
-
-
- Exercise//////////////////////////////////////////////////////////////
-
- mysql> SELECT * FROM PEOPLE WHERE BIRTHDAY IS NOT NULL;
- +----+------------+-----------+----------+------------+---------+
- | id | first_name | last_name | mobile | birthday | home_id |
- +----+------------+-----------+----------+------------+---------+
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 |
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL |
- | 9 | John | Smith | NULL | 1998-04-07 | 4 |
- | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 |
- | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL |
- +----+------------+-----------+----------+------------+---------+
- 10 rows in set (0.00 sec)
-
- 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);
- +------------+-----------+----------+----------------------------------------+
- | first_name | last_name | mon | address |
- +------------+-----------+----------+----------------------------------------+
- | Thomas | Smith | January | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Tony | Carbral | February | 11 Essex Dr.Farmingdale, NY 11735 |
- | John | Smith | April | 234 High Street, PA 19159 |
- | Paul | Thompson | May | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Noelle | Johnson | July | 36 E. Bayberry Rd.Savannah, GA 31404 |
- | Raj | Johnson | August | 920 Arlington Street Clifton, NJ 07011 |
- | Jane | Smith | December | 920 Arlington Street Clifton, NJ 07011 |
- | Doug | Brown | December | 920 Arlington Street Clifton, NJ 07011 |
- +------------+-----------+----------+----------------------------------------+
- 8 rows in set (0.00 sec)
-
- mysql> select * from people left join homes on (people.home_id = homes.id);
- +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
- | id | first_name | last_name | mobile | birthday | home_id | id | address | homenumber |
- +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
- | 3 | Noelle | Johnson | 333-3333 | 1960-07-06 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 4 | Thomas | Smith | 152-9854 | 1955-01-25 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 11 | Paul | Thompson | NULL | 1996-05-27 | 1 | 1 | 36 E. Bayberry Rd.Savannah, GA 31404 | 565-6895 |
- | 1 | Tony | Carbral | 230-4233 | 1970-02-23 | 2 | 2 | 11 Essex Dr.Farmingdale, NY 11735 | 454-4544 |
- | 2 | Raj | Johnson | 333-3333 | 1980-08-31 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- | 5 | Jane | Smith | 152-9854 | 1987-12-06 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- | 6 | Doug | Brown | 466-6241 | 1954-12-07 | 3 | 3 | 920 Arlington Street Clifton, NJ 07011 | 985-4515 |
- | 9 | John | Smith | NULL | 1998-04-07 | 4 | 4 | 234 High Street, PA 19159 | 267-3940 |
- | 7 | John | Smith | 152-9854 | 1973-01-23 | NULL | NULL | NULL | NULL |
- | 8 | Otto | Von Count | 656-6548 | NULL | NULL | NULL | NULL | NULL |
- | 12 | Eli | Kramer | NULL | 1984-01-15 | NULL | NULL | NULL | NULL |
- +----+------------+-----------+----------+------------+---------+------+----------------------------------------+------------+
- 11 rows in set (0.00 sec)
-
- movies Exercise////////////////////////////////
-
- mysql> select * from movies
- -> ;
- +----+-------------------+---------+-------------+------------+--------+
- | id | title | runtime | genre | imdb_score | rating |
- +----+-------------------+---------+-------------+------------+--------+
- | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
- | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
- | 3 | starship_troopers | 129 | sci-fi | 7.2 | pg-13 |
- | 4 | waltz_with_bashir | 90 | documentary | 8.0 | r |
- | 5 | spaceballs | 96 | comedy | 7.1 | pg |
- | 6 | monsters_inc. | 92 | animation | 8.1 | g |
- +----+-------------------+---------+-------------+------------+--------+
- 6 rows in set (0.00 sec)
-
- mysql> select * from movies where genre = 'sci-fi';
- +----+-------------------+---------+--------+------------+--------+
- | id | title | runtime | genre | imdb_score | rating |
- +----+-------------------+---------+--------+------------+--------+
- | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
- | 3 | starship_troopers | 129 | sci-fi | 7.2 | pg-13 |
- +----+-------------------+---------+--------+------------+--------+
- 2 rows in set (0.00 sec)
-
- mysql> select imdb_score from movies where imdb_score >= '6.5';
- +------------+
- | imdb_score |
- +------------+
- | 7.2 |
- | 8.0 |
- | 7.1 |
- | 8.1 |
- +------------+
- 4 rows in set (0.00 sec)
-
- mysql> select title from movies where rating = 'g' or rating = 'pg' and runtime <100;
- +---------------+
- | title |
- +---------------+
- | spaceballs |
- | monsters_inc. |
- +---------------+
- 2 rows in set (0.00 sec)
-
- mysql> select genre, avg(runtime) FROM movies where imdb_score < 7.5 group by genre;
- +--------+--------------+
- | genre | avg(runtime) |
- +--------+--------------+
- | comedy | 96.0000 |
- | horror | 83.0000 |
- | sci-fi | 119.5000 |
- +--------+--------------+
- 3 rows in set (0.00 sec)
-
- mysql> select*from movies;
- +----+-------------------+---------+-------------+------------+--------+
- | id | title | runtime | genre | imdb_score | rating |
- +----+-------------------+---------+-------------+------------+--------+
- | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
- | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
- | 3 | starship_troopers | 129 | sci-fi | 7.2 | r |
- | 4 | waltz_with_bashir | 90 | documentary | 8.0 | r |
- | 5 | spaceballs | 96 | comedy | 7.1 | pg |
- | 6 | monsters_inc. | 92 | animation | 8.1 | g |
- +----+-------------------+---------+-------------+------------+--------+
- 6 rows in set (0.00 sec)
-
- mysql> select id, rating from movies where genre = 'horror' or genre = 'documentary';
- +----+--------+
- | id | rating |
- +----+--------+
- | 2 | tv-14 |
- | 4 | r |
- +----+--------+
- 2 rows in set (0.00 sec)
-
- mysql> select max(imdb_score) from movies where rating = 'pg' ;
- +-----------------+
- | max(imdb_score) |
- +-----------------+
- | 7.1 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select avg(imdb_score) from movies where rating = 'pg' ;
- +-----------------+
- | avg(imdb_score) |
- +-----------------+
- | 5.85000 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select min(imdb_score) from movies where rating = 'pg' ;
- +-----------------+
- | min(imdb_score) |
- +-----------------+
- | 4.6 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select min(imdb_score) from movies where rating = 'g' ;
- +-----------------+
- | min(imdb_score) |
- +-----------------+
- | 8.1 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select max(imdb_score) from movies where rating = 'g' ;
- +-----------------+
- | max(imdb_score) |
- +-----------------+
- | 8.1 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select avg(imdb_score) from movies where rating = 'g' ;
- +-----------------+
- | avg(imdb_score) |
- +-----------------+
- | 8.10000 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select avg(imdb_score) from movies where rating = 'r' ;
- +-----------------+
- | avg(imdb_score) |
- +-----------------+
- | 7.60000 |
- +-----------------+
- 1 row in set (0.01 sec)
-
- mysql> select min(imdb_score) from movies where rating = 'r' ;
- +-----------------+
- | min(imdb_score) |
- +-----------------+
- | 7.2 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select max(imdb_score) from movies where rating = 'r' ;
- +-----------------+
- | max(imdb_score) |
- +-----------------+
- | 8.0 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select max(imdb_score) from movies where rating = 'tv-14' ;
- +-----------------+
- | max(imdb_score) |
- +-----------------+
- | 4.7 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select min(imdb_score) from movies where rating = 'tv-14' ;
- +-----------------+
- | min(imdb_score) |
- +-----------------+
- | 4.7 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select avg(imdb_score) from movies where rating = 'tv-14' ;
- +-----------------+
- | avg(imdb_score) |
- +-----------------+
- | 4.7 |
- +-----------------+
- 1 row in set (0.00 sec)
-
- mysql> select rating from movies group by rating having count(*)>1;
- +--------+
- | rating |
- +--------+
- | pg |
- | r |
- +--------+
- 2 rows in set (0.00 sec)
-
- mysql> delete from movies where rating = 'r';
- Query OK, 2 rows affected (0.01 sec)
-
- mysql> select * from movies;
- +----+-----------------+---------+-----------+------------+--------+
- | id | title | runtime | genre | imdb_score | rating |
- +----+-----------------+---------+-----------+------------+--------+
- | 1 | howard the duck | 110 | sci-fi | 4.6 | pg |
- | 2 | lavalantula | 83 | horror | 4.7 | tv-14 |
- | 5 | spaceballs | 96 | comedy | 7.1 | pg |
- | 6 | monsters_inc. | 92 | animation | 8.1 | g |
- +----+-----------------+---------+-----------+------------+--------+
- 4 rows in set (0.00 sec)
|