MySQL SELECT and SELECT DISTINCT Statements
Show databases and tables
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | DB | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.07 sec) mysql> USE DB; 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_DB | +--------------+ | holders | | loans | +--------------+ 2 rows in set (0.00 sec)
Retrieving all the records from a parent table
mysql> SELECT * FROM holders; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec) mysql> SELECT city FROM holders; +---------+ | city | +---------+ | Phoenix | | Irvine | | Irvine | | Mumbai | | Ribe | | Delhi | | NULL | +---------+ 7 rows in set (0.01 sec)
Retrieving distinct records
mysql> SELECT DISTINCT city FROM holders; +---------+ | city | +---------+ | Phoenix | | Irvine | | Mumbai | | Ribe | | Delhi | | NULL | +---------+ 6 rows in set (0.00 sec) mysql> SELECT DISTINCT name, city FROM holders; +---------------+---------+ | name | city | +---------------+---------+ | James Moore | Phoenix | | Donald Taylor | Irvine | | Edward Parkar | Irvine | | Ryan Bakshi | Mumbai | | Marie Peters | Ribe | | Aanya | Delhi | | James Moore | NULL | +---------------+---------+ 7 rows in set (0.00 sec)
COUNT, MIN, and MAX Functions
mysql> SELECT COUNT(DISTINCT city) AS Count FROM holders; +-------+ | Count | +-------+ | 5 | +-------+ 1 row in set (0.00 sec) mysql> SELECT MIN(amount) FROM holders; +-------------+ | MIN(amount) | +-------------+ | 5000 | +-------------+ 1 row in set (0.00 sec) mysql> SELECT MAX(amount) FROM holders; +-------------+ | MAX(amount) | +-------------+ | 105000 | +-------------+ 1 row in set (0.00 sec)
Retrieving all the records from a child table
mysql> SELECT * FROM loans; +---------+-----------+-------------+-------------+ | loan_id | loan_type | loan_amount | account_no | +---------+-----------+-------------+-------------+ | 1 | Personal | 5660 | 25622348989 | | 2 | Home | 450000 | 25622348992 | | 3 | Car | 600000 | 25622348994 | | 4 | Home | 40000 | 25622348995 | +---------+-----------+-------------+-------------+ 4 rows in set (0.00 sec) mysql> DELETE FROM loans WHERE loan_id=4; Query OK, 1 row affected (0.17 sec) mysql> SELECT * FROM loans; +---------+-----------+-------------+-------------+ | loan_id | loan_type | loan_amount | account_no | +---------+-----------+-------------+-------------+ | 1 | Personal | 5660 | 25622348989 | | 2 | Home | 450000 | 25622348992 | | 3 | Car | 600000 | 25622348994 | +---------+-----------+-------------+-------------+ 3 rows in set (0.00 sec) mysql> INSERT INTO loans(loan_type,loan_amount,account_no) VALUES ("Home",40000,25622348995); Query OK, 1 row affected (0.19 sec) mysql> SELECT * FROM loans; +---------+-----------+-------------+-------------+ | loan_id | loan_type | loan_amount | account_no | +---------+-----------+-------------+-------------+ | 1 | Personal | 5660 | 25622348989 | | 2 | Home | 450000 | 25622348992 | | 3 | Car | 600000 | 25622348994 | | 5 | Home | 40000 | 25622348995 | +---------+-----------+-------------+-------------+ 4 rows in set (0.00 sec)
Advertisement