0% found this document useful (0 votes)
6 views

SQL Practicals (1)

Uploaded by

Pawan Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Practicals (1)

Uploaded by

Pawan Choudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

mysql> show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| aniket |
| chada |
| computer |
| geetesh |
| geetesh1 |
| harshit |
+--------------------+
7 rows in set (0.01 sec)

mysql> use geetesh1;


Database changed

mysql> show tables;


Empty set (0.00 sec)

mysql> create table student(rollno int, name varchar (25), gender varchar(5), marks decimal (7,3));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into student values(1001,'rahul','male',65.8);


Query OK, 1 row affected (0.05 sec)

mysql> insert into student values(1002,'rajat','male',78.6);


Query OK, 1 row affected (0.02 sec)

mysql> insert into student values(1003,'ruhi','f',92.1);


Query OK, 1 row affected (0.03 sec)

mysql> insert into student values(1004,'rashi','f',56);


Query OK, 1 row affected (0.02 sec)

mysql> insert into student values(1005,'raju','male',88);


Query OK, 1 row affected (0.01 sec)

mysql> select * from student;


+--------+-------+--------+--------+
| rollno | name | gender | marks |
+--------+-------+--------+--------+
| 1001 | rahul | male | 65.800 |
| 1002 | rajat | male | 78.600 |
| 1003 | ruhi | f | 92.100 |
| 1004 | rashi | f | 56.000 |
| 1005 | raju | male | 88.000 |
+--------+-------+--------+--------+
5 rows in set (0.00 sec)

mysql> select * from student order by marks;


+--------+-------+--------+--------+
| rollno | name | gender | marks |
+--------+-------+--------+--------+
| 1004 | rashi | f | 56.000 |
| 1001 | rahul | male | 65.800 |
| 1002 | rajat | male | 78.600 |
| 1005 | raju | male | 88.000 |
| 1003 | ruhi | f | 92.100 |
+--------+-------+--------+--------+
5 rows in set (0.05 sec)

mysql> select * from student order by marks desc;


+--------+-------+--------+--------+
| rollno | name | gender | marks |
+--------+-------+--------+--------+
| 1003 | ruhi | f | 92.100 |
| 1005 | raju | male | 88.000 |
| 1002 | rajat | male | 78.600 |
| 1001 | rahul | male | 65.800 |
| 1004 | rashi | f | 56.000 |
+--------+-------+--------+--------+
5 rows in set (0.00 sec)

mysql> alter table student add games varchar(20);


Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from student;


+--------+-------+--------+--------+-------+
| rollno | name | gender | marks | games |
+--------+-------+--------+--------+-------+
| 1001 | rahul | male | 65.800 | NULL |
| 1002 | rajat | male | 78.600 | NULL |
| 1003 | ruhi | f | 92.100 | NULL |
| 1004 | rashi | f | 56.000 | NULL |
| 1005 | raju | male | 88.000 | NULL |
+--------+-------+--------+--------+-------+
5 rows in set (0.00 sec)

mysql> alter table student drop games;


Query OK, 5 rows affected (0.08 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from student;


+--------+-------+--------+--------+
| rollno | name | gender | marks |
+--------+-------+--------+--------+
| 1001 | rahul | male | 65.800 |
| 1002 | rajat | male | 78.600 |
| 1003 | ruhi | f | 92.100 |
| 1004 | rashi | f | 56.000 |
| 1005 | raju | male | 88.000 |
+--------+-------+--------+--------+
5 rows in set (0.00 sec)

mysql> select rollno, name, marks from student where marks>70 and marks<99;
+--------+-------+--------+
| rollno | name | marks |
+--------+-------+--------+
| 1002 | rajat | 78.600 |
| 1003 | ruhi | 92.100 |
| 1005 | raju | 88.000 |
+--------+-------+--------+
3 rows in set (0.06 sec)

mysql> select rollno, name, marks from student where marks>70 or marks<99;
+--------+-------+--------+
| rollno | name | marks |
+--------+-------+--------+
| 1001 | rahul | 65.800 |
| 1002 | rajat | 78.600 |
| 1003 | ruhi | 92.100 |
| 1004 | rashi | 56.000 |
| 1005 | raju | 88.000 |
+--------+-------+--------+
5 rows in set (0.00 sec)

mysql> select * from student where name is not null;


+--------+-------+--------+--------+
| rollno | name | gender | marks |
+--------+-------+--------+--------+
| 1001 | rahul | male | 65.800 |
| 1002 | rajat | male | 78.600 |
| 1003 | ruhi | f | 92.100 |
| 1004 | rashi | f | 56.000 |
| 1005 | raju | male | 88.000 |
+--------+-------+--------+--------+
5 rows in set (0.00 sec)

mysql> select * from student where name is null;


Empty set (0.00 sec)

mysql> select rollno, name, marks from student where marks between 70 and 99;
+--------+-------+--------+
| rollno | name | marks |
+--------+-------+--------+
| 1002 | rajat | 78.600 |
| 1003 | ruhi | 92.100 |
| 1005 | raju | 88.000 |
+--------+-------+--------+
3 rows in set (0.03 sec)

mysql> select rollno, name, marks from student where name in("rahul" , "ruhi");
+--------+-------+--------+
| rollno | name | marks |
+--------+-------+--------+
| 1001 | rahul | 65.800 |
| 1003 | ruhi | 92.100 |
+--------+-------+--------+
2 rows in set (0.05 sec)
mysql> select avg(marks)"average" from student;
+------------+
| average |
+------------+
| 76.1000000 |
+------------+
1 row in set (0.03 sec)

mysql> select count(*) "total" from student;


+-------+
| total |
+-------+
| 5|
+-------+
1 row in set (0.05 sec)

mysql> select max(marks) "maximum marks" from student;


+---------------+
| maximum marks |
+---------------+
| 92.100 |
+---------------+
1 row in set (0.00 sec)

mysql> select min(marks) "minimum marks" from student;


+---------------+
| minimum marks |
+---------------+
| 56.000 |
+---------------+
1 row in set (0.00 sec)

mysql> select sum(marks) "total marks" from student;


+-------------+
| total marks |
+-------------+
| 380.500 |
+-------------+
1 row in set (0.02 sec)

mysql> create table employee2(ecode int(11),lastname varchar(25),firstname varch


ar(30),department varchar(30),salary int(11));
Query OK, 0 rows affected (0.02 )

mysql> alter table employee2 add depcode int;


Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from department,employee2;


+-------+------------+-------+----------+-----------+--------+---------+
| dcode | dname | ecode | lastname | firstname | salary | depcode |
+-------+------------+-------+----------+-----------+--------+---------+
| 1 | production | 105 | raghav | riya | 62000 | NULL |
| 2 | sales | 105 | raghav | riya | 62000 | NULL |
| 3 | sales | 105 | raghav | riya | 62000 | NULL |
| 4 | production | 105 | raghav | riya | 62000 | NULL |
| 5 | finance | 105 | raghav | riya | 62000 | NULL |
+-------+------------+-------+----------+-----------+--------+---------+
5 rows in set (0.00 sec)
mysql> update department set dcode=dcode+101;
Query OK, 5 rows affected (0.02 sec)
Rows matched: 5 Changed: 5 Warnings: 0

mysql> select *from department;


+-------+------------+
| dcode | dname |
+-------+------------+
| 102 | production |
| 103 | sales |
| 104 | sales |
| 105 | production |
| 106 | finance |
+-------+------------+
5 rows in set (0.00 sec)

mysql> update employee2 set depcode=102;


Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select *from employee2;


+-------+----------+-----------+--------+---------+
| ecode | lastname | firstname | salary | depcode |
+-------+----------+-----------+--------+---------+
| 105 | raghav | riya | 62000 | 102 |
+-------+----------+-----------+--------+---------+
1 row in set (0.00 sec)

mysql> insert into employee2 values(101,'kumar','rajat',61000,104);


Query OK, 1 row affected (0.02 sec)

mysql> insert into employee2 values(102,'sachdeva','vishal',34500,104);


Query OK, 1 row affected (0.03 sec)

mysql> insert into employee2 values(103,'bhalla','raju',51000,105);


Query OK, 1 row affected (0.02 sec)

mysql> insert into employee2 values(104,'khosla','prateek',50000,103);


Query OK, 1 row affected (0.02 sec)

mysql> select *from employee2;


+-------+----------+-----------+--------+---------+
| ecode | lastname | firstname | salary | depcode |
+-------+----------+-----------+--------+---------+
| 105 | raghav | riya | 62000 | 102 |
| 101 | kumar | rajat | 61000 | 104 |
| 102 | sachdeva | vishal | 34500 | 104 |
| 103 | bhalla | raju | 51000 | 105 |
| 104 | khosla | prateek | 50000 | 103 |
+-------+----------+-----------+--------+---------+
5 rows in set (0.00 sec)

mysql> delete from employee2 where lastname="raghav";


Query OK, 1 row affected (0.03 sec)

mysql> select * from employee2,department where employee2.depcode=department.dcode;


+-------+----------+-----------+--------+---------+-------+------------+
| ecode | lastname | firstname | salary | depcode | dcode | dname |
+-------+----------+-----------+--------+---------+-------+------------+
| 105 | raghav | riya | 62000 | 102 | 102 | production |
| 104 | khosla | prateek | 50000 | 103 | 103 | sales |
| 101 | kumar | rajat | 61000 | 104 | 104 | sales |
| 102 | sachdeva | vishal | 34500 | 104 | 104 | sales |
| 103 | bhalla | raju | 51000 | 105 | 105 | production |
+-------+----------+-----------+--------+---------+-------+------------+
5 rows in set (0.00 sec)

mysql> select sum(salary) from employee2;


+-------------+
| sum(salary) |
+-------------+
| 258500 |
+-------------+
1 row in set (0.00 sec)

mysql> select min(salary) from employee2;


+-------------+
| min(salary) |
+-------------+
| 34500 |
+-------------+
1 row in set (0.00 sec)

mysql> select max(salary) from employee2;


+-------------+
| max(salary) |
+-------------+
| 62000 |
+-------------+
1 row in set (0.00 sec)

mysql> select avg(salary) from employee2;


+-------------+
| avg(salary) |
+-------------+
| 51700.0000 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(salary) from employee2;


+---------------+
| count(salary) |
+---------------+
| 5|
+---------------+
1 row in set (0.00 sec)

mysql> select count(*) from employee2;


+----------+
| count(*) |
+----------+
| 5|
+----------+
1 row in set (0.00 sec)

mysql> update department set dname="marketing" where dcode=103;


Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> update department set dname="administration" where dcode=102;


Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select dname,sum(salary) from employee2,department where department.dcode


=employee2.depcode group by depcode;
+----------------+-------------+
| dname | sum(salary) |
+----------------+-------------+
| administration | 62000 |
| marketing | 50000 |
| sales | 95500 |
| production | 51000 |
+----------------+-------------+
4 rows in set (0.00 sec)

mysql> select dname,sum(salary) from employee2,department where department.dcode


=employee2.depcode group by depcode having count(depcode)>1;
+-------+-------------+
| dname | sum(salary) |
+-------+-------------+
| sales | 95500 |
+-------+-------------+
1 row in set (0.00 sec)

You might also like