SQL Tutorial Cheat Sheet
SQL Tutorial Cheat Sheet
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \c
mysql> clrscr
->
->
->
-> clrscr;
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 'clrscr
clrscr' at line 1
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql>
mysql> INSERT INTO STUDENT VALUES
-> ('GAURAV', 'JUJGAR', 'ghostrider96@gmail.com',
-> '789 three st',
-> 'Pune',
-> 'MH',
-> 411030,
-> '4588626510',
-> "1996-9-15",
-> 'M',
-> NOW(),
-> 150,
-> NULL);
Query OK, 1 row affected (0.36 sec)
mysql>
mysql> select * from student;
+------------+------------+--------------------------+--------------+------+-------
+--------+------------+------------+-----+---------------------+------------
+------------+
| first_name | last_name | email | street | city | state
| zip | phone | birth_date | sex | date_entered | lunch_cost |
student_id |
+------------+------------+--------------------------+--------------+------+-------
+--------+------------+------------+-----+---------------------+------------
+------------+
| OMCAR | UDAWANT | omkarudawant2@gmail.com | 123 main st | Pune | MH
| 411028 | 9822529493 | 1997-06-22 | M | 2018-02-07 19:11:47 | 100 |
1 |
| SAMIKSHA | BELGAONKAR | samikshab14@gmail.com | 489 one st | Pune | MH
| 411027 | 1234567890 | 1996-11-14 | F | 2018-02-07 19:26:48 | 1000 |
2 |
| ADITYA | UDAWANT | adityaudawant2@gmail.com | 456 two st | Pune | MH
| 411029 | 9881436800 | 2001-04-08 | M | 2018-02-07 19:27:00 | 110 |
3 |
| GAURAV | JUJGAR | ghostrider96@gmail.com | 789 three st | Pune | MH
| 411030 | 4588626510 | 1996-09-15 | M | 2018-02-07 19:27:08 | 150 |
4 |
+------------+------------+--------------------------+--------------+------+-------
+--------+------------+------------+-----+---------------------+------------
+------------+
4 rows in set (0.00 sec)
mysql>
mysql> INSERT INTO CLASS VALUES
-> ('ENLISH', NULL), ('MATHS', NULL),('LITERATURE', NULL),('PHYSICS', NULL),
('CHEMISTRY', NULL),('HISTORY', NULL),
->
-> ('GYM', NULL);
Query OK, 7 rows affected (0.27 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> INSERT INTO TEST VALUES
-> ('2018-2-25','T', 30, 1, NULL),
-> ('2018-2-29','T', 30, 1, NULL),
-> ('2018-2-21','Q', 30, 2, NULL),
-> ('2018-2-20','T', 30, 3, NULL),
-> ('2018-2-22','Q', 30, 1, NULL);
ERROR 1292 (22007): Incorrect date value: '2018-2-29' for column 'date' at row 2
mysql>
mysql> INSERT INTO TEST VALUES
-> ('2018-1-25','T', 30, 1, NULL),
-> ('2018-1-29','T', 30, 1, NULL),
-> ('2018-1-21','Q', 30, 2, NULL),
-> ('2018-1-20','T', 30, 3, NULL),
-> ('2018-1-22','Q', 30, 1, NULL);
Query OK, 5 rows affected (0.73 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT * FROM TEST;
+------------+------+----------+----------+---------+
| date | type | maxscore | class_id | test_id |
+------------+------+----------+----------+---------+
| 2018-01-25 | T | 30 | 1 | 6 |
| 2018-01-29 | T | 30 | 1 | 7 |
| 2018-01-21 | Q | 30 | 2 | 8 |
| 2018-01-20 | T | 30 | 3 | 9 |
| 2018-01-22 | Q | 30 | 1 | 10 |
+------------+------+----------+----------+---------+
5 rows in set (0.00 sec)
mysql> SELECT
-> TEST_ID AS TEST,
-> MIN(SCORE) AS MIN,
-> MAX(SCORE) AS MAX,
-> MAX(SCORE)-MIN(SCORE) AS RANGE,
-> SUM(SCORE) AS TOTAL,
-> AVG(SCORE) AS AVERAGE
-> FROM SCORES
-> GROUP BY TEST_ID;
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 'RANGE,
SUM(SCORE) AS TOTAL,
AVG(SCORE) AS AVERAGE
FROM SCORES
GROUP BY TEST_ID' at line 5
mysql> SELECT
-> test_id AS TEST,
-> MAX(SCORE) AS MAX,
-> MIN(SCORE) AS MIN,
-> MAX(SCORE)-MIN(SCORE) AS RANGE,
-> SUM(SCORE) AS TOTAL,
-> AVG(SCORE) AS AVERAGE
-> FROM SCORES
-> GROUP BY TEST_ID;
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 'RANGE,
SUM(SCORE) AS TOTAL,
AVG(SCORE) AS AVERAGE
FROM SCORES
GROUP BY TEST_ID' at line 5
mysql> SELECT
-> TEST_ID AS TEST,
-> MIN(SCORE) AS MIN,
-> MAX(SCORE) AS MAX,
-> MAX(SCORE)-MIN(SCORE) AS RANGE,
-> SUM(SCORE) AS TOTAL,
-> AVG(SCORE) AS AVERAGE
-> FROM SCORES
-> GROUP BY test_id;
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 'RANGE,
SUM(SCORE) AS TOTAL,
AVG(SCORE) AS AVERAGE
FROM SCORES
GROUP BY test_id' at line 5
mysql> desc score;
ERROR 1146 (42S02): Table 'omkar.score' doesn't exist
mysql> desc scores;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| test_id | int(10) unsigned | NO | PRI | NULL | |
| score | int(11) | NO | | NULL | |
+------------+------------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> select
-> test_id as TEst,
-> min(score) as MIN,
-> max(score) as MAX,
-> (max(score)-min(score)) as range,
-> sum(score) as total,
-> avg(score) as average
-> from scores
-> group by test_id;
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 'range,
sum(score) as total,
avg(score) as average
from scores
group by test_id' at line 5
mysql>
mysql> select
-> test_id as 'TEst',
-> min(score) as 'MIN',
-> max(score) as 'MAX',
-> max(score)-min(score) as 'range',
-> sum(score) as 'total',
-> avg(score) as 'average'
-> from scores
-> group by test_id;
+------+------+------+-------+-------+---------+
| TEst | MIN | MAX | range | total | average |
+------+------+------+-------+-------+---------+
| 6 | 20 | 22 | 2 | 62 | 20.6667 |
| 7 | 20 | 20 | 0 | 20 | 20.0000 |
| 9 | 20 | 28 | 8 | 69 | 23.0000 |
| 10 | 26 | 29 | 3 | 55 | 27.5000 |
+------+------+------+-------+-------+---------+
4 rows in set (0.10 sec)
ERROR:
No query specified
mysql> alter table absences add column test_taken CHAR(1) NOT NULL DEFAULT 'F'
AFTER student_id;
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
ERROR:
No query specified
ERROR:
No query specified
mysql>