SQL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

Enter password: ****

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

Your MySQL connection id is 8

Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 student_db;

Database changed

mysql> show tables;

+----------------------+

| Tables_in_student_db |

+----------------------+

| book |

| census |

| lolol |

| student |

| student_info |

+----------------------+

5 rows in set (0.05 sec)

mysql> create table marks(Roll_No int primary key, Name varchar(33), Narks int);

Query OK, 0 rows affected (0.04 sec)

mysql> insert into marks values(1,"Yash Tandon", 96)


-> ;

Query OK, 1 row affected (0.01 sec)

mysql> insert into marks values(2,"Rahul", 45);

Query OK, 1 row affected (0.00 sec)

mysql> select * from book;

+---------+----------------------+---------+------------+------+

| BOOK_ID | BOOK_NAME | Roll_No | Book_Price | NOP |

+---------+----------------------+---------+------------+------+

| 1001 | Harry potter | 2| 100 | 500 |

| 1002 | Percy Jackson | 5| 100 | 500 |

| 1003 | Alex rider | 1| 100 | 500 |

| 1004 | Hunger Games | 14 | 100 | 500 |

| 1005 | Who killed evans | 15 | 100 | 500 |

| 1006 | Wimpy kid | 11 | 100 | 500 |

| 1007 | Middle School | 7| 100 | 500 |

| 1008 | Goosebumps | 8| 100 | 500 |

| 1009 | Red white Royal Blue | 9| 100 | 500 |

| 1010 | Star Wars | 10 | 100 | 500 |

| 1011 | Alladin | 10 | 100 | 500 |

+---------+----------------------+---------+------------+------+

11 rows in set (0.00 sec)

mysql> desc book;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| BOOK_ID | int | NO | PRI | NULL | |

| BOOK_NAME | varchar(50) | YES | | NULL | |

| Roll_No | int | YES | MUL | NULL | |


| Book_Price | int | YES | | NULL | |

| NOP | int | YES | | 500 | |

+------------+-------------+------+-----+---------+-------+

5 rows in set (0.01 sec)

mysql> create table BOOJ

-> ;

ERROR 4028 (HY000): A table must have at least one visible column.

mysql> CREATE TABLE BOOK_INFO(BOOK_ID varchar(10) primary key, BOOK_NAME varchar, Roll_No
int, foriegn key Roll_No references marks(Roll_No));

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 ', Roll_No int, foriegn key Roll_No
references marks(Roll_No))' at line 1

mysql> ;

ERROR:

No query specified

mysql> CREATE TABLE BOOK_INFO(BOOK_ID varchar(10) primary key, BOOK_NAME varchar(67),


Roll_No int, foriegn key (Roll_No) references marks(Roll_No));

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 'key (Roll_No) references
marks(Roll_No))' at line 1

mysql> CREATE TABLE BOOK_INFO(BOOK_ID varchar(10) primary key, BOOK_NAME varchar(67),


Roll_No int, foreign key (Roll_No) references marks(Roll_No));

Query OK, 0 rows affected (0.01 sec)

mysql> select * from marks

-> ;

+---------+-------------+-------+

| Roll_No | Name | Narks |

+---------+-------------+-------+

| 1 | Yash Tandon | 96 |

| 2 | Rahul | 45 |
+---------+-------------+-------+

2 rows in set (0.00 sec)

mysql> insert into BOOK_INFO values("A1001", "Harry Potter", 3);

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`student_db`.`book_info`, CONSTRAINT `book_info_ibfk_1` FOREIGN KEY (`Roll_No`) REFERENCES
`marks` (`Roll_No`))

mysql> insert into BOOK_INFO values("A1001", "Harry Potter", 1);

Query OK, 1 row affected (0.00 sec)

mysql> insert into BOOK_INFO values("A1001", "Harry Potter", 2);

ERROR 1062 (23000): Duplicate entry 'A1001' for key 'book_info.PRIMARY'

mysql> insert into BOOK_INFO values("A1002", "Percy Jackson", 2);

Query OK, 1 row affected (0.00 sec)

mysql> select * from marks;

+---------+-------------+-------+

| Roll_No | Name | Narks |

+---------+-------------+-------+

| 1 | Yash Tandon | 96 |

| 2 | Rahul | 45 |

+---------+-------------+-------+

2 rows in set (0.00 sec)

mysql> select * from BOOK_INFO;

+---------+---------------+---------+

| BOOK_ID | BOOK_NAME | Roll_No |

+---------+---------------+---------+

| A1001 | Harry Potter | 1|

| A1002 | Percy Jackson | 2|

+---------+---------------+---------+

2 rows in set (0.00 sec)


mysql> delect from from marks where Roll_no="12";

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 'delect from from marks where
Roll_no="12"' at line 1

mysql> delete from from marks where Roll_no="12";

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 'from marks where Roll_no="12"' at line 1

mysql> delete from from marks where Roll_no=1;

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 'from marks where Roll_no=1' at line 1

mysql> delete from from marks where Roll_no="1";

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 'from marks where Roll_no="1"' at line 1

mysql> delete from marks where Roll_no="1";

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`student_db`.`book_info`, CONSTRAINT `book_info_ibfk_1` FOREIGN KEY (`Roll_No`) REFERENCES
`marks` (`Roll_No`))

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql> DELETE FROM BOOK_INNFO WHERE Roll_No="1";

ERROR 1146 (42S02): Table 'student_db.book_innfo' doesn't exist

mysql> DELETE FROM BOOK_INFO WHERE Roll_No="1";

Query OK, 1 row affected (0.00 sec)


mysql> select * from BOOK_INFO;

+---------+---------------+---------+

| BOOK_ID | BOOK_NAME | Roll_No |

+---------+---------------+---------+

| A1002 | Percy Jackson | 2|

+---------+---------------+---------+

1 row in set (0.00 sec)

mysql> delete from marks where Roll_no="1";

Query OK, 1 row affected (0.00 sec)

mysql> select * from marks;

+---------+-------+-------+

| Roll_No | Name | Narks |

+---------+-------+-------+

| 2 | Rahul | 45 |

+---------+-------+-------+

1 row in set (0.00 sec)

mysql> show tables;

+----------------------+

| Tables_in_student_db |

+----------------------+

| book |

| book_info |

| census |

| lolol |

| marks |

| student |

| student_info |

+----------------------+
7 rows in set (0.00 sec)

mysql> select * from student_info;

+---------+-------------+-------+

| Roll_No | Name | Marks |

+---------+-------------+-------+

| 1 | Yash Tandon | 10 |

| 2 | Rahul | 10 |

| 3 | Gal | 10 |

| 4 | Rishi | 10 |

| 5 | Ria | 10 |

| 6 | Tanya | 10 |

| 7 | Archit | 10 |

| 8 | 1000 | 10 |

| 9 | 200czx | 10 |

| 10 | 300czx | 10 |

| 11 | 3000 | 10 |

| 13 | 0 | 10 |

| 14 | yash | 10 |

| 15 | NULL | 10 |

| 16 | Luffy | 10 |

| 17 | Tanjiro | 10 |

| 18 | Naruto | 10 |

| 19 | lolol | 10 |

+---------+-------------+-------+

18 rows in set (0.01 sec)

mysql> show databses;

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 'databses' at line 1

mysql> show databases;


+--------------------+

| Database |

+--------------------+

| employee |

| information_schema |

| mysql |

| performance_schema |

| sakila |

| student |

| student_db |

| sys |

| world |

+--------------------+

9 rows in set (0.00 sec)

mysql> use employee;

Database changed

mysql> show tables;

+--------------------+

| Tables_in_employee |

+--------------------+

| emp |

| emp1 |

| emp2 |

| emp3 |

| emp4 |

| product |

+--------------------+

6 rows in set (0.00 sec)

mysql> select * from emp;


+-------+-------+--------+-------+-------+

| Ecode | Ename | Gender | Grade | Gross |

+-------+-------+--------+-------+-------+

| 100 | Yash | M |A | 2000 |

| 101 | Rahul | M |B | 200 |

| 102 | Tanya | F |A | 20000 |

+-------+-------+--------+-------+-------+

3 rows in set (0.00 sec)

mysql> select * from emp1;

Empty set (0.00 sec)

mysql> select * from emp2;

Empty set (0.00 sec)

mysql> select * from product;

+-------+-------+

| Pcode | Price |

+-------+-------+

| 101 | 250 |

| 101 | 5000 |

+-------+-------+

2 rows in set (0.00 sec)

mysql> select distinct count(Gender) from emp

-> ;

+---------------+

| count(Gender) |

+---------------+

| 3|

+---------------+
1 row in set (0.00 sec)

mysql> select count(distinct Gender)) from emp

-> ;

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 ') from emp' at line 1

mysql> select count(distinct Gender) from emp;

+------------------------+

| count(distinct Gender) |

+------------------------+

| 2|

+------------------------+

1 row in set (0.00 sec)

mysql> select * from emp GROUP BY Gender;

+-------+-------+--------+-------+-------+

| Ecode | Ename | Gender | Grade | Gross |

+-------+-------+--------+-------+-------+

| 100 | Yash | M |A | 2000 |

| 102 | Tanya | F |A | 20000 |

+-------+-------+--------+-------+-------+

2 rows in set (0.00 sec)

mysql> select count from emp GROUP BY Gender;

ERROR 1054 (42S22): Unknown column 'count' in 'field list'

mysql> select count(Gender) from emp GROUP BY Gender;

+---------------+

| count(Gender) |

+---------------+

| 2|

| 1|
+---------------+

2 rows in set (0.00 sec)

mysql> select Gender, count(Gender) from emp GROUP BY Gender;

+--------+---------------+

| Gender | count(Gender) |

+--------+---------------+

|M | 2|

|F | 1|

+--------+---------------+

2 rows in set (0.00 sec)

mysql> select * from emp;

+-------+-------+--------+-------+-------+

| Ecode | Ename | Gender | Grade | Gross |

+-------+-------+--------+-------+-------+

| 100 | Yash | M |A | 2000 |

| 101 | Rahul | M |B | 200 |

| 102 | Tanya | F |A | 20000 |

+-------+-------+--------+-------+-------+

3 rows in set (0.00 sec)

mysql> alter table emp add(Dept_No int);

Query OK, 0 rows affected (0.01 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from emp;

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | NULL |


| 101 | Rahul | M |B | 200 | NULL |

| 102 | Tanya | F |A | 20000 | NULL |

+-------+-------+--------+-------+-------+---------+

3 rows in set (0.00 sec)

mysql> update emp set Dept_No=10;

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3 Changed: 3 Warnings: 0

mysql> select * from emp;

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

+-------+-------+--------+-------+-------+---------+

3 rows in set (0.00 sec)

mysql> insert into emp values(103,"Archit", "M","A", 2839, 12);

Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(103,"Ria", "F","C", 282, 11);

ERROR 1062 (23000): Duplicate entry '103' for key 'emp.PRIMARY'

mysql> insert into emp values(104,"Ria", "F","C", 282, 11);

Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(105,"Khushi", "F","B", 2822, 10);

Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;


+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select Gender, count(Gender) from emp GROUP BY Gender;

+--------+---------------+

| Gender | count(Gender) |

+--------+---------------+

|M | 3|

|F | 3|

+--------+---------------+

2 rows in set (0.00 sec)

mysql> select Dept_No, count(Dept_No) from emp GROUP BY Dept_No;

+---------+----------------+

| Dept_No | count(Dept_No) |

+---------+----------------+

| 10 | 4|

| 12 | 1|

| 11 | 1|

+---------+----------------+

3 rows in set (0.00 sec)


mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select Dept_No, count(Dept_No) as "No_of_employee", SUM(Gross) from emp GROUP BY


Dept_No;

+---------+----------------+------------+

| Dept_No | No_of_employee | SUM(Gross) |

+---------+----------------+------------+

| 10 | 4| 25022 |

| 12 | 1| 2839 |

| 11 | 1| 282 |

+---------+----------------+------------+

3 rows in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |


| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select* from emp where Ename="%Y";

Empty set (0.00 sec)

mysql> select* from emp where Ename="Y%";

Empty set (0.00 sec)

mysql> select* from emp where Ename like "Y%";

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

+-------+-------+--------+-------+-------+---------+

1 row in set (0.00 sec)

mysql> select* from emp where Ename like "a%";

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 103 | Archit | M |A | 2839 | 12 |

+-------+--------+--------+-------+-------+---------+

1 row in set (0.00 sec)

mysql> select* from emp where Ename like "%a";

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |


+-------+-------+--------+-------+-------+---------+

| 102 | Tanya | F |A | 20000 | 10 |

| 104 | Ria | F |C | 282 | 11 |

+-------+-------+--------+-------+-------+---------+

2 rows in set (0.00 sec)

mysql> select* from emp where Ename like "_a%";

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

+-------+-------+--------+-------+-------+---------+

3 rows in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> s

-> ;
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 's' at line 1

mysql> select * from emp ORDER BY Dept_No;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 105 | Khushi | F |B | 2822 | 10 |

| 104 | Ria | F |C | 282 | 11 |

| 103 | Archit | M |A | 2839 | 12 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select * from emp ORDER BY Dept_No desc

-> ;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+
| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select count(Ename) GROUP BY Grade having Dept_No=10;

ERROR 1054 (42S22): Unknown column 'Ename' in 'field list'

mysql> select count(Ecode) GROUP BY Grade having Dept_No=10;

ERROR 1054 (42S22): Unknown column 'Ecode' in 'field list'

mysql> select count(Grade) GROUP BY Grade having Dept_No=10;

ERROR 1054 (42S22): Unknown column 'Grade' in 'field list'

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql> select count(Grade) from emp GROUP BY Grade having Dept_No=10;

ERROR 1054 (42S22): Unknown column 'Dept_No' in 'having clause'


mysql> select count(Grade) from emp GROUP BY Grade having "Dept_No"=10;

Empty set, 1 warning (0.00 sec)

mysql> select count(Grade) from emp GROUP BY Grade;

+--------------+

| count(Grade) |

+--------------+

| 3|

| 2|

| 1|

+--------------+

3 rows in set (0.00 sec)

mysql> select Grade, count(Grade) from emp GROUP BY Grade;

+-------+--------------+

| Grade | count(Grade) |

+-------+--------------+

|A | 3|

|B | 2|

|C | 1|

+-------+--------------+

3 rows in set (0.00 sec)

mysql> select Grade, count(Grade), Dept_No from emp GROUP BY Grade having Dept_No=10;

+-------+--------------+---------+

| Grade | count(Grade) | Dept_No |

+-------+--------------+---------+

|A | 3| 10 |

|B | 2| 10 |

+-------+--------------+---------+

2 rows in set (0.00 sec)


mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select * max(Gross) from emp;

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 'max(Gross) from emp' at line 1

mysql> select max(Gross) from emp;

+------------+

| max(Gross) |

+------------+

| 20000 |

+------------+

1 row in set (0.00 sec)

mysql> select * from

-> emp where Gross=20000;

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 102 | Tanya | F |A | 20000 | 10 |


+-------+-------+--------+-------+-------+---------+

1 row in set (0.00 sec)

mysql> select * from emp where Gross=(select max(Gross) from emp);

+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 102 | Tanya | F |A | 20000 | 10 |

+-------+-------+--------+-------+-------+---------+

1 row in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select count(*) from emp;

+----------+

| count(*) |

+----------+

| 6|

+----------+

1 row in set (0.00 sec)


mysql> select count(Grade) from emp;

+--------------+

| count(Grade) |

+--------------+

| 6|

+--------------+

1 row in set (0.00 sec)

mysql> select count(distinct Grade) from emp;

+-----------------------+

| count(distinct Grade) |

+-----------------------+

| 3|

+-----------------------+

1 row in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> select * from emp where Gross=(select min(Gross) from emp);


+-------+-------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+-------+--------+-------+-------+---------+

| 101 | Rahul | M |B | 200 | 10 |

+-------+-------+--------+-------+-------+---------+

1 row in set (0.00 sec)

mysql> select AVG(Gross) from emp;

+------------+

| AVG(Gross) |

+------------+

| 4690.5000 |

+------------+

1 row in set (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 2000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> update emp set Gross=5000 where Ecode=100;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0


mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 5000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 10 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)

mysql> update emp set Dept_No=12 where Ecode=102;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 5000 | 10 |

| 101 | Rahul | M |B | 200 | 10 |

| 102 | Tanya | F |A | 20000 | 12 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

6 rows in set (0.00 sec)


mysql> delete from emp where Ecode=101;

Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 5000 | 10 |

| 102 | Tanya | F |A | 20000 | 12 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

5 rows in set (0.00 sec)

mysql> desc emp

-> ;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| Ecode | int | NO | PRI | NULL | |

| Ename | varchar(50) | NO | | NULL | |

| Gender | char(5) | NO | | NULL | |

| Grade | char(5) | YES | | NULL | |

| Gross | int | YES | | NULL | |

| Dept_No | int | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

mysql> Alter table emp modify (Dept_No not 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 '(Dept_No not null)' at line 1

mysql> Alter table emp modify (Dept_No NOT 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 '(Dept_No NOT NULL)' at line 1

mysql> Alter table emp modify (Dept_No,NOT 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 '(Dept_No,NOT NULL)' at line 1

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql> Alter table emp modify (Dept_No,NOT 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 '(Dept_No,NOT NULL)' at line 1

mysql> Alter table emp modify (Dept_No,NOT 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 '(Dept_No,NOT NULL)' at line 1

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>
mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql>

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 5000 | 10 |

| 102 | Tanya | F |A | 20000 | 12 |

| 103 | Archit | M |A | 2839 | 12 |

| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

5 rows in set (0.00 sec)


mysql> Alter table emp modify (Dept_No NOT 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 '(Dept_No NOT NULL)' at line 1

mysql> DESC EMP;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| Ecode | int | NO | PRI | NULL | |

| Ename | varchar(50) | NO | | NULL | |

| Gender | char(5) | NO | | NULL | |

| Grade | char(5) | YES | | NULL | |

| Gross | int | YES | | NULL | |

| Dept_No | int | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

6 rows in set (0.00 sec)

mysql> Alter table EMP modify(Gender char(1));

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 '(Gender char(1))' at line 1

mysql> Alter table EMP modify(Gender varchar(2));

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 '(Gender varchar(2))' at line 1

mysql> alter table emp add constraint EN_NOTNULL (Dept_No NOT 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 '(Dept_No NOT NULL)' at line 1

mysql> select * from emp;

+-------+--------+--------+-------+-------+---------+

| Ecode | Ename | Gender | Grade | Gross | Dept_No |

+-------+--------+--------+-------+-------+---------+

| 100 | Yash | M |A | 5000 | 10 |

| 102 | Tanya | F |A | 20000 | 12 |

| 103 | Archit | M |A | 2839 | 12 |


| 104 | Ria | F |C | 282 | 11 |

| 105 | Khushi | F |B | 2822 | 10 |

+-------+--------+--------+-------+-------+---------+

5 rows in set (0.00 sec)

mysql> SELECT Gross+Dept_No from emp;

+---------------+

| Gross+Dept_No |

+---------------+

| 5010 |

| 20012 |

| 2851 |

| 293 |

| 2832 |

+---------------+

5 rows in set (0.00 sec)

mysql> show databses;

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 'databses' at line 1

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| employee |

| information_schema |

| mysql |

| performance_schema |

| sakila |

| student |

| student_db |
| sys |

| world |

+--------------------+

9 rows in set (0.00 sec)

mysql> use student_db

Database changed

mysql> show tables;

+----------------------+

| Tables_in_student_db |

+----------------------+

| book |

| book_info |

| census |

| lolol |

| marks |

| student |

| student_info |

+----------------------+

7 rows in set (0.00 sec)

mysql> select * from marks;

+---------+-------+-------+

| Roll_No | Name | Narks |

+---------+-------+-------+

| 2 | Rahul | 45 |

+---------+-------+-------+

1 row in set (0.00 sec)

mysql> select * from srudent;

ERROR 1146 (42S02): Table 'student_db.srudent' doesn't exist


mysql> select * from student;

+-----+-------+---------+-------------+------+-------------+

| SNo | Class | Name | GAME4 | G1 | Game2 |

+-----+-------+---------+-------------+------+-------------+

| 7 | 10 | Arpit | Cricket | A | Gardening |

| 8| 9 | Archana | Basket Ball | A | Literature |

| 9| 7 | Veena | Tennis | C | Cooking |

| 10 | 7 | Kamal | Swimming | B | Photography |

| 11 | 8 | Sujit | Tennis | A | Gardening |

| 12 | 7 | Sameer | Cricket | B | Photography |

+-----+-------+---------+-------------+------+-------------+

6 rows in set (0.00 sec)

mysql> select * from student_info;

+---------+-------------+-------+

| Roll_No | Name | Marks |

+---------+-------------+-------+

| 1 | Yash Tandon | 10 |

| 2 | Rahul | 10 |

| 3 | Gal | 10 |

| 4 | Rishi | 10 |

| 5 | Ria | 10 |

| 6 | Tanya | 10 |

| 7 | Archit | 10 |

| 8 | 1000 | 10 |

| 9 | 200czx | 10 |

| 10 | 300czx | 10 |

| 11 | 3000 | 10 |

| 13 | 0 | 10 |

| 14 | yash | 10 |

| 15 | NULL | 10 |
| 16 | Luffy | 10 |

| 17 | Tanjiro | 10 |

| 18 | Naruto | 10 |

| 19 | lolol | 10 |

+---------+-------------+-------+

18 rows in set (0.00 sec)

mysql> show tables;

+----------------------+

| Tables_in_student_db |

+----------------------+

| book |

| book_info |

| census |

| lolol |

| marks |

| student |

| student_info |

+----------------------+

7 rows in set (0.00 sec)

mysql> select *from censusl

-> ;

ERROR 1146 (42S02): Table 'student_db.censusl' doesn't exist

mysql> select *from census;

+-----------------+------------+------------+---------+

| name | DOB | DOD | Salary |

+-----------------+------------+------------+---------+

| Yash Tandon | 0000-00-00 | 0000-00-00 | 10 |

| Rahul Diwan | 0000-00-00 | NULL | 10000 |

| Archit Banerjee | 0000-00-00 | NULL | 1000000 |


| Rishi Chand | 0000-00-00 | 0000-00-00 | 78000 |

| Ria Sharma | 0000-00-00 | NULL | NULL |

| Ria Sharma | 2007-04-06 | NULL | NULL |

| Yash Tandon | 2005-03-29 | 2005-03-29 | 10 |

| Tanya Vnajara | 2005-04-21 | NULL | 0|

+-----------------+------------+------------+---------+

8 rows in set (0.01 sec)

mysql> select* from lolol

-> ;

+---------+------+

| Roll_No | Name |

+---------+------+

| 1 | Yash |

+---------+------+

1 row in set (0.00 sec)

mysql> select * BOOK_INFO;

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 'BOOK_INFO' at line 1

mysql> select * FROM BOOK_INFO;

+---------+---------------+---------+

| BOOK_ID | BOOK_NAME | Roll_No |

+---------+---------------+---------+

| A1002 | Percy Jackson | 2|

+---------+---------------+---------+

1 row in set (0.00 sec)

mysql> select * FROM BOOK;

+---------+----------------------+---------+------------+------+

| BOOK_ID | BOOK_NAME | Roll_No | Book_Price | NOP |


+---------+----------------------+---------+------------+------+

| 1001 | Harry potter | 2| 100 | 500 |

| 1002 | Percy Jackson | 5| 100 | 500 |

| 1003 | Alex rider | 1| 100 | 500 |

| 1004 | Hunger Games | 14 | 100 | 500 |

| 1005 | Who killed evans | 15 | 100 | 500 |

| 1006 | Wimpy kid | 11 | 100 | 500 |

| 1007 | Middle School | 7| 100 | 500 |

| 1008 | Goosebumps | 8| 100 | 500 |

| 1009 | Red white Royal Blue | 9| 100 | 500 |

| 1010 | Star Wars | 10 | 100 | 500 |

| 1011 | Alladin | 10 | 100 | 500 |

+---------+----------------------+---------+------------+------+

11 rows in set (0.00 sec)

mysql> SELECT * FROM MARKS

-> ;

+---------+-------+-------+

| Roll_No | Name | Narks |

+---------+-------+-------+

| 2 | Rahul | 45 |

+---------+-------+-------+

1 row in set (0.00 sec)

mysql> DESC MARKS

-> ;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| Roll_No | int | NO | PRI | NULL | |

| Name | varchar(33) | YES | | NULL | |


| Narks | int | YES | | NULL | |

+---------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> DESC BOOK_INFO

-> ;

+-----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+

| BOOK_ID | varchar(10) | NO | PRI | NULL | |

| BOOK_NAME | varchar(67) | YES | | NULL | |

| Roll_No | int | YES | MUL | NULL | |

+-----------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> select * FROM BOOK_INFO;

+---------+---------------+---------+

| BOOK_ID | BOOK_NAME | Roll_No |

+---------+---------------+---------+

| A1002 | Percy Jackson | 2|

+---------+---------------+---------+

1 row in set (0.00 sec)

mysql> SELECT * FROM MARKS'

'> ;

'> ;

'> ;

'> ;

'> ;)

'> \C

'> ):
'>

You might also like