Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
227 views

SQL Tutorial Cheat Sheet

The document shows various SQL commands being run in a MySQL database session. It demonstrates connecting to the database, creating and selecting from databases and tables, and inserting data into a table.

Uploaded by

OMKAR UDAWANT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
227 views

SQL Tutorial Cheat Sheet

The document shows various SQL commands being run in a MySQL database session. It demonstrates connecting to the database, creating and selecting from databases and tables, and inserting data into a table.

Uploaded by

OMKAR UDAWANT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Enter password: **********

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 7
Server version: 5.7.21-log MySQL Community Server (GPL)

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> mysql -u root -p Omkar@1997


->
-> mysql -u root -p Omkar@1997;
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 'mysql -u
root -p Omkar@1997

mysql -u root -p Omkar@1997' at line 1


mysql> mysql root Omkar@1997;
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 'mysql
root Omkar@1997' at line 1
mysql> show databases
-> show databases;
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 'show
databases' at line 2
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.03 sec)

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> create database Omkar;


Query OK, 1 row affected (1.46 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| omkar |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)

mysql> use omkar;


Database changed
mysql> select database
-> select database;
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 'select
database' at line 2
mysql> select database;
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
mysql> select database();
+------------+
| database() |
+------------+
| omkar |
+------------+
1 row in set (0.00 sec)

mysql> create table student


-> (first_name varchar(30) NOT NULL,
-> last_name varchar(30) NOT NULL,
-> email varchar(60) NULL,
-> street varchar(40) NOT NULL,
-> city varchar(40) NOT NULL,
-> state CHAR(2) NOT NULL DEFAULT "MH",
-> zip MEDIUMINT UNSIGNED NOT NULL,
-> phone varchar(20) NOT NULL,
-> birth_date DATE NOT NULL,
-> sex ENUM('M', 'F') NOT NULL,
-> date_entered TIMESTAMP,
-> lunch_cost FLOAT NULL,
-> student_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (21.46 sec)
mysql>
mysql> show tables;
+-----------------+
| Tables_in_omkar |
+-----------------+
| student |
+-----------------+
1 row in set (0.02 sec)

mysql> DESC STUDENT;


+--------------+-----------------------+------+-----+-------------------
+-----------------------------+
| Field | Type | Null | Key | Default | Extra
|
+--------------+-----------------------+------+-----+-------------------
+-----------------------------+
| first_name | varchar(30) | NO | | NULL |
|
| last_name | varchar(30) | NO | | NULL |
|
| email | varchar(60) | YES | | NULL |
|
| street | varchar(40) | NO | | NULL |
|
| city | varchar(40) | NO | | NULL |
|
| state | char(2) | NO | | MH |
|
| zip | mediumint(8) unsigned | NO | | NULL |
|
| phone | varchar(20) | NO | | NULL |
|
| birth_date | date | NO | | NULL |
|
| sex | enum('M','F') | NO | | NULL |
|
| date_entered | timestamp | NO | | CURRENT_TIMESTAMP | on update
CURRENT_TIMESTAMP |
| lunch_cost | float | YES | | NULL |
|
| student_id | int(10) unsigned | NO | PRI | NULL |
auto_increment |
+--------------+-----------------------+------+-----+-------------------
+-----------------------------+
13 rows in set (0.51 sec)

mysql> INSERT INTO STUDENT VALUES


-> ('OMCAR', 'UDAWANT', 'omkarudawant2@gmail.com',
-> '123 main st',
-> 'Pune',
-> 'MH',
-> 411028,
-> '9822529493',
-> "1997-6-22",
-> 'M',
-> NOW(),
-> 100,
-> NULL);
Query OK, 1 row affected (0.63 sec)
mysql> SELECT * FORM STUDENT;
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 'FORM
STUDENT' at line 1
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 |
+------------+-----------+-------------------------+-------------+------+-------
+--------+------------+------------+-----+---------------------+------------
+------------+
1 row in set (0.00 sec)

mysql> INSERT INTO STUDENT VALUES


-> -> ('OMCAR', 'UDAWANT', 'omkarudawant2@gmail.com',
-> -> '123 main st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411028,
-> -> '9822529493',
-> -> "1997-6-22",
-> -> 'M',
-> -> NOW(),
-> -> 100,
-> -> NULL);INSERT INTO STUDENT VALUES
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 '->
('OMCAR', 'UDAWANT', 'omkarudawant2@gmail.com',
-> '123 main st',
-> ' at line 2
-> -> ('OMCAR', 'UDAWANT', 'omkarudawant2@gmail.com',
-> -> '123 main st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411028,
-> -> '9822529493',
-> -> "1997-6-22",
-> -> 'M',
-> -> NOW(),
-> -> 100,
->
->
-> );
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 '->
('OMCAR', 'UDAWANT', 'omkarudawant2@gmail.com',
-> '123 main st',
-> ' at line 2
mysql> INSERT INTO STUDENT VALUES
-> -> ('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> -> '123 main st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411028,
-> -> '1234567890',
-> -> "1996-11-14",
-> -> 'F',
-> -> NOW(),
-> -> 100,
-> -> NULL);
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 '->
('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> '123 main st',
' at line 2
mysql>
mysql> INSERT INTO STUDENT VALUES
-> -> ('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> -> '489 main st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411027,
-> -> '1234567890',
-> -> "1996-11-14",
-> -> 'F',
-> -> NOW(),
-> -> 1000,
-> -> NULL);
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 '->
('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> '489 main st',
' at line 2
mysql>
mysql> INSERT INTO STUDENT VALUES
-> -> ('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> -> '489 main st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411027,
-> -> '1234567890',
-> -> "1996-11-14",
-> -> 'F',
-> -> NOW(),
-> -> 1000,
-> -> 1000,
->
-> ;
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 '->
('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> '489 main st',
' at line 2
mysql> INSERT INTO STUDENT VALUES
-> -> ('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> -> '489 one st',
-> -> 'Pune',
-> -> 'MH',
-> -> 411027,
-> -> '1234567890',
-> -> "1996-11-14",
-> -> 'F',
-> -> NOW(),
-> -> 1000,
-> -> NULL);
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 '->
('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> '489 one st',
' at line 2
mysql>
mysql> INSERT INTO STUDENT VALUES
-> ('SAMIKSHA', 'BELGAONKAR', 'samikshab14@gmail.com',
-> '489 one st',
-> 'Pune',
-> 'MH',
-> 411027,
-> '1234567890',
-> "1996-11-14",
-> 'F',
-> NOW(),
-> 1000,
-> NULL);
Query OK, 1 row affected (0.42 sec)

mysql> INSERT INTO STUDENT VALUES


-> ('ADITYA', 'UDAWANT', 'adityaudawant2@gmail.com',
-> '456 two st',
-> 'Pune',
-> 'MH',
-> 411029,
-> '9881436800',
-> "2001-4-8",
-> 'M',
-> NOW(),
-> 110,
-> NULL);
Query OK, 1 row affected (0.60 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> create table class(


-> name varchar(30) NOT NULL,
-> class_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (1.57 sec)

mysql> SHOW TABLES;


+-----------------+
| Tables_in_omkar |
+-----------------+
| class |
| student |
+-----------------+
2 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> SELECT * FROM CLASS;


+------------+----------+
| name | class_id |
+------------+----------+
| ENLISH | 1 |
| MATHS | 2 |
| LITERATURE | 3 |
| PHYSICS | 4 |
| CHEMISTRY | 5 |
| HISTORY | 6 |
| GYM | 7 |
+------------+----------+
7 rows in set (0.00 sec)

mysql> CREATE TABLE TEST(


-> date DATE NOT NULL,
-> type ENUM('T', 'Q') NOT NULL,
-> class_id INT UNSIGNED NOT NULL,
-> test_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (1.02 sec)

mysql> SHOW TABLES;


+-----------------+
| Tables_in_omkar |
+-----------------+
| class |
| student |
| test |
+-----------------+
3 rows in set (0.00 sec)

mysql> create table score(


-> student_id INT UNSIGNED NOT NULL,
-> event_id INT UNSIGNED NOT NULL,
-> score INT NOT NULL,
-> PRIMARY KEY(event_id, student_id));
Query OK, 0 rows affected (1.05 sec)

mysql> create table absence(


-> student_id INT UNSIGNED NOT NULL,
-> date DATE NOT NULL,
-> PRIMARY KEY(student_id, date));
Query OK, 0 rows affected (1.13 sec)

mysql> show tables;


+-----------------+
| Tables_in_omkar |
+-----------------+
| absence |
| class |
| score |
| student |
| test |
+-----------------+
5 rows in set (0.00 sec)

mysql> desc test;


+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| date | date | NO | | NULL | |
| type | enum('T','Q') | NO | | NULL | |
| class_id | int(10) unsigned | NO | | NULL | |
| test_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+----------+------------------+------+-----+---------+----------------+
4 rows in set (0.06 sec)

mysql> alter table test


-> add maxscore INT NOT NULL AFTER type;
Query OK, 0 rows affected (2.18 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc test;


+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| date | date | NO | | NULL | |
| type | enum('T','Q') | NO | | NULL | |
| maxscore | int(11) | NO | | NULL | |
| class_id | int(10) unsigned | NO | | NULL | |
| test_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+----------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

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> ALTER TABLE SCORE CHANGE event_id test_id


-> INT UNSIGNED NOT NULL;
Query OK, 0 rows affected (0.75 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> DESC SCORE;


+------------+------------------+------+-----+---------+-------+
| 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.00 sec)
mysql>
mysql> INSERT INTO SCORE VALUES
-> (1,6,20),
-> (1,7,20),
-> (1,9,20),
-> (2,6,22),
-> (2,10,29),
-> (3,9,28),
-> (4,6,20),
-> (5,9,21),
-> (6,10,26);
Query OK, 9 rows affected (0.20 sec)
Records: 9 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM SCORE;


+------------+---------+-------+
| student_id | test_id | score |
+------------+---------+-------+
| 1 | 6 | 20 |
| 2 | 6 | 22 |
| 4 | 6 | 20 |
| 1 | 7 | 20 |
| 1 | 9 | 20 |
| 3 | 9 | 28 |
| 5 | 9 | 21 |
| 2 | 10 | 29 |
| 6 | 10 | 26 |
+------------+---------+-------+
9 rows in set (0.00 sec)

mysql> DESC ABSENCE;


+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| date | date | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
2 rows in set (0.25 sec)

mysql> INSERT INTO ABSENCE VALUES


-> (6,'2018-1-20'),
-> (5,'2018-1-21'),
-> (2,'2018-1-25'),
-> (3,'2018-1-29');
Query OK, 4 rows affected (0.23 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> SELECT FIRST_NAME, LAST_NAME


-> FROM STUDENT;
+------------+------------+
| FIRST_NAME | LAST_NAME |
+------------+------------+
| OMCAR | UDAWANT |
| SAMIKSHA | BELGAONKAR |
| ADITYA | UDAWANT |
| GAURAV | JUJGAR |
+------------+------------+
4 rows in set (0.05 sec)
mysql> RENAME TABLE
-> ABSENCE TO ABSENCES,
-> CLASS TO CLASSES,
-> SCORE TO SCORES,
-> STUDENT TO STUDENTS,
-> TEST TO TESTS;
Query OK, 0 rows affected (1.48 sec)

mysql> SHOW TABLES;


+-----------------+
| Tables_in_omkar |
+-----------------+
| absences |
| classes |
| scores |
| students |
| tests |
+-----------------+
5 rows in set (0.00 sec)

mysql> SELECT FIRST_NAME FROM STUDENTS


-> WHERE YEAR(BIRTH_DATE) >= 2000;
+------------+
| FIRST_NAME |
+------------+
| ADITYA |
+------------+
1 row in set (0.06 sec)

mysql> SELECT LAST_NAME


-> FROM STUDENTS
-> ORDER BY FIRST_NAME;
+------------+
| LAST_NAME |
+------------+
| UDAWANT |
| JUJGAR |
| UDAWANT |
| BELGAONKAR |
+------------+
4 rows in set (0.04 sec)

mysql> SELECT FIRST_NAME


-> FROM STUDENTS
-> LIMIT 2;
+------------+
| FIRST_NAME |
+------------+
| OMCAR |
| SAMIKSHA |
+------------+
2 rows in set (0.00 sec)

mysql> SELECT CONCAT(FIRST_NAME, " ", LAST_NAME) AS "Name",


-> CONCAT(CITY, " ", STATE) AS Hometown
-> from students;
+---------------------+----------+
| Name | Hometown |
+---------------------+----------+
| OMCAR UDAWANT | Pune MH |
| SAMIKSHA BELGAONKAR | Pune MH |
| ADITYA UDAWANT | Pune MH |
| GAURAV JUJGAR | Pune MH |
+---------------------+----------+
4 rows in set (0.02 sec)

mysql> select first_name


-> from students
-> where first_name like 'O%' OR last_name '%T';
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 ''%T'' at
line 3
mysql> where first_name like 'O%' OR last_name like '%T';
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 'where
first_name like 'O%' OR last_name like '%T'' at line 1
mysql> select first_name
-> from students
-> where first_name LIKE 'O%' OR last_name LIKE '%T';
+------------+
| first_name |
+------------+
| OMCAR |
| ADITYA |
+------------+
2 rows in set (0.00 sec)

mysql> SELECT FIRST_NAME


-> FROM STUDENTS
-> WHERE LAST_NAME LIKE '______T';
+------------+
| FIRST_NAME |
+------------+
| OMCAR |
| ADITYA |
+------------+
2 rows in set (0.00 sec)

mysql> SELECT DISTINCT FIRST_NAME


-> FROM STUDENTS
-> ORDER BY LAST_NAME;
+------------+
| FIRST_NAME |
+------------+
| SAMIKSHA |
| GAURAV |
| OMCAR |
| ADITYA |
+------------+
4 rows in set (0.11 sec)

mysql> SELECT COUNT(DISTINCT STATE)


-> FROM STUDENTS;
+-----------------------+
| COUNT(DISTINCT STATE) |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.07 sec)

mysql> SELECT COUNT(*)


-> FORM STUDENTS;
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
'STUDENTS' at line 2
mysql> SELECT COUNT(*)
-> FROM STUDENTS;
+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(*)


-> FROM STUDENTS
-> WHERE SEX='M';
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT SEX, COUNT(*)


-> FROM STUDENTS
-> GROUP BY SEX;
+-----+----------+
| SEX | COUNT(*) |
+-----+----------+
| M | 3 |
| F | 1 |
+-----+----------+
2 rows in set (0.00 sec)

mysql> SELECT MONTH(BIRTH_DATE) AS MONTH, COUNT(*)


-> FROM STUDENTS
-> GROUP BY MONTH
-> ORDER BY MONTH;
+-------+----------+
| MONTH | COUNT(*) |
+-------+----------+
| 4 | 1 |
| 6 | 1 |
| 9 | 1 |
| 11 | 1 |
+-------+----------+
4 rows in set (0.02 sec)

mysql> SELECT STATE, COUNT(STATE) AS AMOUNT


-> FROM STUDENTS
-> GROUP BY STATE
-> HAVING AMOUNT > 1;
+-------+--------+
| STATE | AMOUNT |
+-------+--------+
| MH | 4 |
+-------+--------+
1 row 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> 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)

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.00 sec)

mysql> desc absense;


ERROR 1146 (42S02): Table 'omkar.absense' doesn't exist
mysql> desc absences;;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| date | date | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

ERROR:
No query specified

mysql> select student_id, test_id


-> from scores
-> where student_id = 5;
+------------+---------+
| student_id | test_id |
+------------+---------+
| 5 | 9 |
+------------+---------+
1 row in set (0.03 sec)

mysql> insert into scores values (5,6,25);


Query OK, 1 row affected (0.21 sec)

mysql> select student_id, test_id


-> from scores
-> where student_id = 5;
+------------+---------+
| student_id | test_id |
+------------+---------+
| 5 | 6 |
| 5 | 9 |
+------------+---------+
2 rows in set (0.00 sec)

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

mysql> desc absences;;


+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| test_taken | char(1) | NO | | F | |
| date | date | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

ERROR:
No query specified

mysql> alter table absences


-> modify column test_taken ENUM('T', 'F') NOT NULL DEFAULT 'F';
Query OK, 4 rows affected (1.21 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> desc absences;;


+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| test_taken | enum('T','F') | NO | | F | |
| date | date | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
ERROR:
No query specified

mysql> alter table absences


-> drop column test_taken;
Query OK, 0 rows affected (0.84 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc absences;;


+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO | PRI | NULL | |
| date | date | NO | PRI | NULL | |
+------------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

ERROR:
No query specified

mysql> select students.student_id,


-> concat(students.first_name, " ", students.last_name) as 'NAME',
-> count(absences.date) as 'Absences'
-> from students left join absences
-> on students.student_id = absences.student_id
-> group by students.student_id;
+------------+---------------------+----------+
| student_id | NAME | Absences |
+------------+---------------------+----------+
| 1 | OMCAR UDAWANT | 0 |
| 2 | SAMIKSHA BELGAONKAR | 1 |
| 3 | ADITYA UDAWANT | 1 |
| 4 | GAURAV JUJGAR | 0 |
+------------+---------------------+----------+
4 rows in set (0.04 sec)

mysql> select * from absences;


+------------+------------+
| student_id | date |
+------------+------------+
| 2 | 2018-01-25 |
| 3 | 2018-01-29 |
| 5 | 2018-01-21 |
| 6 | 2018-01-20 |
+------------+------------+
4 rows in set (0.00 sec)

mysql> select students.first_name,


-> students.last_name,
-> scores.test_id,
-> scores.score
-> from students
-> inner join scores
-> on students.student_id = scores.student.id
-> where scores.score <= 25
-> order by scores.test_id;
ERROR 1054 (42S22): Unknown column 'scores.student.id' in 'on clause'
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.00 sec)

mysql> select students.first_name,


-> students.last_name,
-> scores.test_id,
-> scores.score
-> from students
-> inner join scores
-> on students.student_id = scores.student_id
-> where scores.score <= 25
-> order by scores.test_id;
+------------+------------+---------+-------+
| first_name | last_name | test_id | score |
+------------+------------+---------+-------+
| OMCAR | UDAWANT | 6 | 20 |
| GAURAV | JUJGAR | 6 | 20 |
| SAMIKSHA | BELGAONKAR | 6 | 22 |
| OMCAR | UDAWANT | 7 | 20 |
| OMCAR | UDAWANT | 9 | 20 |
+------------+------------+---------+-------+
5 rows in set (0.03 sec)

mysql>

You might also like