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

SQL

The document outlines the creation and manipulation of two databases: 'students' and 'department'. It details the creation of tables, insertion of records, updates, and various SQL queries to retrieve specific data, including averages and conditions. Additionally, it covers the establishment of relationships between tables and the use of aggregate functions.

Uploaded by

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

SQL

The document outlines the creation and manipulation of two databases: 'students' and 'department'. It details the creation of tables, insertion of records, updates, and various SQL queries to retrieve specific data, including averages and conditions. Additionally, it covers the establishment of relationships between tables and the use of aggregate functions.

Uploaded by

alexthomasatk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Create table students with fields sno , sname, sex, mark with sno as
primary key and assign suitable constraints for each attribute.
Insert five records into the table.

mysql> create database students;


Query OK, 1 row affected (0.17 sec)

mysql> use students;


Database changed

mysql> create table students(sno int primary key,sname varchar(20),sex


varchar(10),mark int);
Query OK, 0 rows affected (0.15 sec)

mysql> insert into students values(101,"arun","m",50);


Query OK, 1 row affected (0.03 sec)

mysql> insert into students values(102,"priya","f",55);


Query OK, 1 row affected (0.01 sec)

mysql> insert into students values(103,"fathima","f",45);


Query OK, 1 row affected (0.01 sec)

mysql> insert into students values(104,"ajay","m",43);


Query OK, 1 row affected (0.01 sec)

mysql> insert into students values(105,"arya","f",39);


Query OK, 1 row affected (0.01 sec)

mysql> select * from students;


+-----+---------+------+------+
| sno | sname | sex | mark |
+-----+---------+------+------+
| 101 | arun | m | 50 |
| 102 | priya | f | 55 |
| 103 | fathima | f | 45 |
| 104 | ajay | m | 43 |
| 105 | arya | f | 39 |
+-----+---------+------+------+
5 rows in set (0.01 sec)

• Alter the table by adding one more field rank


mysql> alter table students add position int;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update students set position=1 where sname="priya";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update students set position=2 where sname="arun";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update students set position=3 where sname="fathima";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update students set position=4 where sname="ajay";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update students set position=5 where sname="arya";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from students;


+-----+---------+------+------+----------+
| sno | sname | sex | mark | position |
+-----+---------+------+------+----------+
| 101 | arun |m | 50 | 2|
| 102 | priya | f | 55 | 1|
| 103 | fathima | f | 45 | 3|
| 104 | ajay |m | 43 | 4|
| 105 | arya |f | 39 | 5|
+-----+---------+------+------+----------+

• Display all boy students with their name.

mysql> select sname from students where sex="m";


+-------+
| sname |
+-------+
| arun |
| ajay |
+-------+
2 rows in set (0.01 sec)

• Find the average mark.

mysql> select avg(mark) from students;


+-----------+
| avg(mark) |
+-----------+
| 46.4000 |
+-----------+
1 row in set (0.02 sec)

• Create a query to display the sno and sname for all students who
got more than the average mark. Sorts the results in descending
order of mark.

mysql> select sno,sname from students where mark>(select avg(mark)from students);


+-----+-------+
| sno | sname |
+-----+-------+
| 101 | arun |
| 102 | priya |
+-----+-------+
2 rows in set (0.01 sec)

• Display all girl student names for those who have marks greater
than 20 and less than 40.
mysql> select sname from students where sex="f" and mark>20 and mark<40;
+-------+
| sname |
+-------+
| arya |
+-------+
1 row in set (0.00 sec)
2. Create table department with fields ename, salary, dno, dname,
place with dno as primary key. Insert five records into the table.

mysql> create table department( dno int primary key,ename varchar(20),salary int,dname
varchar(20),place varchar(20));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into department values(101,"amaya",3000,"commerce","kannur");


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(102,"albin",6000,"cs","kannur");


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(103,"krishna",5000,"cs","pala");


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(104,"mariya",10000,"hrm","payannur");


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(105,"john",6000,"commerce","kannur");


Query OK, 1 row affected (0.01 sec)

mysql> select * from department;


+-----+---------+--------+----------+----------+
| dno | ename | salary | dname | place |
+-----+---------+--------+----------+----------+
| 101 | amaya | 3000 | commerce | kannur |
| 102 | albin | 6000 | cs | kannur |
| 103 | krishna | 5000 | cs | pala |
| 104 | mariya | 10000 | hrm | payannur |
| 105 | john | 6000 | commerce | kannur |
+-----+---------+--------+----------+----------+
5 rows in set (0.00 sec)

• Rename the field ‘place’ with ‘city’.


mysql> alter table department change place city varchar(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from department;


+-----+---------+--------+----------+----------+
| dno | ename | salary | dname | city |
+-----+---------+--------+----------+----------+
| 101 | amaya | 3000 | commerce | kannur |
| 102 | albin | 6000 | cs | kannur |
| 103 | krishna | 5000 | cs | pala |
| 104 | mariya | 10000 | hrm | payannur |
| 105 | john | 6000 | commerce | kannur |
+-----+---------+--------+----------+----------+
5 rows in set (0.00 sec)

• Display the employees who got salary more than Rs.6000 and less
than Rs.10000.

mysql> select * from department where salary >6000 and salary <11000;
+-----+--------+--------+-------+----------+
| dno | ename | salary | dname | city |
+-----+--------+--------+-------+----------+
| 104 | mariya | 10000 | hrm | payannur |
+-----+--------+--------+-------+----------+
1 row in set (0.00 sec)

• Display the total salary of the organization.

mysql> select sum(salary) from department;


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

• Display ename for those who are getting salary in between 5000
and 10000.

mysql> select ename from department where salary between 5000


and 10000;
+---------+
| ename |
+---------+
| albin |
| krishna |
| mariya |
| john |
+---------+
4 rows in set (0.00 sec)

• Create a view named ‘star’ with fields ename ,salary & place.

mysql> create view star as select ename,salary,city from department;


Query OK, 0 rows affected (0.02 sec)

mysql> select * from star;


+---------+--------+----------+
| ename | salary | city |
+---------+--------+----------+
| amaya | 3000 | kannur |
| albin | 6000 | kannur |
| krishna | 5000 | pala |
| mariya | 10000 | payannur |
| john | 6000 | kannur |
+---------+--------+----------+
5 rows in set (0.01 sec)

• Display ename and salary with salary rounded with 10 digits.

mysql> select ename, ROUND(salary,10) as roundsalary from department;


+---------+-------------+
| ename | roundsalary |
+---------+-------------+
| amaya | 3000 |
| albin | 6000 |
| krishna | 5000 |
| mariya | 10000 |
| john | 6000 |
+---------+-------------+
5 rows in set (0.01 sec)

3. Create a table department with fields dno,dname,dmanager and


place with dno as primary key.

Create a table emp with fields eno,ename,job,dno,salary,with eno as


primary key.Set dno as foregin key.

Insert five records into each table.

mysql> create database department;


Query OK, 1 row affected (0.03 sec)

mysql> use department;


Database changed

mysql> insert into department values(1,"computer science","arun","kannur");


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values(2,"commerce","john","kasaragod");


Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(3,"management


studies","priya","payannur"
);
Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(4,"computer


studies","sneha","malapuram")
;
Query OK, 1 row affected (0.01 sec)

mysql> insert into department values(5,"accounting","arjun","palakad");


Query OK, 1 row affected (0.01 sec)

mysql> select * from department;


+-----+--------------------+----------+-----------+
| dno | dname | dmanager | place |
+-----+--------------------+----------+-----------+
| 1 | computer science | arun | kannur |
| 2 | commerce | john | kasaragod |
| 3 | management studies | priya | payannur |
| 4 | computer studies | sneha | malapuram |
| 5 | accounting | arjun | palakad |
+-----+--------------------+----------+-----------+
5 rows in set (0.01 sec)

mysql> create table emp(eno int primary key,ename varchar(20),job


varchar(20),dno int,foreign key(dno) references department(dno),salary int);
Query OK, 0 rows affected (0.05 sec)

mysql> insert into emp values(10,"athira","assistant professor",1,5000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(20,"anagha","assistant professor",2,6000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(30,"maya","assistant professor",3,5500);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(40,"varun","assistant professor",4,7000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(50,"keerthi","assistant professor",5,7500);


Query OK, 1 row affected (0.01 sec)

mysql> select * from emp;


+-----+---------+---------------------+------+--------+
| eno | ename | job | dno | salary |
+-----+---------+---------------------+------+--------+
| 10 | athira | assistant professor | 1 | 5000 |
| 20 | anagha | assistant professor | 2 | 6000 |
| 30 | maya | assistant professor | 3 | 5500 |
| 40 | varun | assistant professor | 4 | 7000 |
| 50 | keerthi | assistant professor | 5 | 7500 |
+-----+---------+---------------------+------+--------+
5 rows in set (0.00 sec)

• Display the ename and salary,salary with ascending order

mysql> select ename,salary from emp order by salary asc;


+---------+--------+
| ename | salary |
+---------+--------+
| athira | 5000 |
| maya | 5500 |
| anagha | 6000 |
| varun | 7000 |
| keerthi | 7500 |
+---------+--------+
5 rows in set (0.01 sec)

• Display ename and salary for eno=20

mysql> select ename,salary from emp where eno=20;


+--------+--------+
| ename | salary |
+--------+--------+
| anagha | 6000 |
+--------+--------+
1 row in set (0.01 sec)

• Display the manager for the accounting department.

mysql> select dmanager from department where dname="accounting";


+----------+
| dmanager |
+----------+
| arjun |
+----------+
1 row in set (0.01 sec)

• Display the name,salary and manager of all employees who are


getting salary >5000.

mysql> select emp.ename,department.dmanager,emp.salary from emp join


department on department.dno=emp.dno where salary>5000;
+---------+----------+--------+
| ename | dmanager | salary |
+---------+----------+--------+
| anagha | john | 6000 |
| maya | priya | 5500 |
| varun | sneha | 7000 |
| keerthi | arjun | 7500 |
+---------+----------+--------+
4 rows in set (0.02 sec)

• Write the queries using various group functions.

mysql> select ename,job from emp group by ename,job;


+---------+---------------------+
| ename | job |
+---------+---------------------+
| athira | assistant professor |
| anagha | assistant professor |
| maya | assistant professor |
| varun | assistant professor |
| keerthi | assistant professor |
+---------+---------------------+
5 rows in set (0.01 sec)

mysql> select ename, job from emp group by eno;


+---------+---------------------+
| ename | job |
+---------+---------------------+
| athira | assistant professor |
| anagha | assistant professor |
| maya | assistant professor |
| varun | assistant professor |
| keerthi | assistant professor |
+---------+---------------------+
5 rows in set (0.00 sec)

• Write the queries using various Number functions

mysql> select avg(salary) from emp;


+-------------+
| avg(salary) |
+-------------+
| 6200.0000 |
+-------------+
1 row in set (0.01 sec)

mysql> select sum(salary) from emp;


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

mysql> select min(salary) from emp;


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

mysql> select max(salary) from emp;


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

4. Create a table emp with fields eno,ename, job, manager and salary,
with eno as primary key. Insert values into the table.
mysql> create database employee;
Query OK, 1 row affected (0.01 sec)

mysql> use employee;


Database changed
mysql> create table emp(eno int primary key,ename varchar(25),job
varchar(20),manager varchar(20),salary int);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into emp values(1,"athira","it","asst",20000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(2,"amal","it","asst",24000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(3,"dayana","it","asst",22000);


Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(4,"don","it","asst",26000);


Query OK, 1 row affected (0.01 sec)
mysql> insert into emp values(5,"joseph","it","asst",25000);
Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(6,"ajay","it","asst",30000);


Query OK, 1 row affected (0.01 sec)

mysql> select * from emp;


+-----+--------+------+---------+--------+
| eno | ename | job | manager | salary |
+-----+--------+------+---------+--------+
| 1 | athira | it | asst | 20000 |
| 2 | amal | it | asst | 24000 |
| 3 | dayana | it | asst | 22000 |
| 4 | don | it | asst | 26000 |
| 5 | joseph | it | asst | 25000 |
| 6 | ajay | it | asst | 30000 |
+-----+--------+------+---------+--------+
6 rows in set (0.00 sec)

• Display ename, salary from emp who are getting salary more
than average salary of the organization.

mysql> select ename,salary from emp where salary > (select AVG(salary)from
emp);
+--------+--------+
| ename | salary |
+--------+--------+
| don | 26000 |
| joseph | 25000 |
| ajay | 30000 |
+--------+--------+
3 rows in set (0.00 sec)

• ADD 20%DA as extra salary to all employees. Label the coloumn


as 'New Salary'.

mysql> alter table emp add new_salary int;


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

mysql> update emp set new_salary=salary+(salary*.2);


Query OK, 6 rows affected (0.01 sec)
Rows matched: 6 Changed: 6 Warnings: 0

mysql> select * from emp;


+-----+--------+------+---------+--------+------------+
| eno | ename | job | manager | salary | new_salary |
+-----+--------+------+---------+--------+------------+
| 1 | athira | it | asst | 20000 | 24000 |
| 2 | amal | it | asst | 24000 | 28800 |
| 3 | dayana | it | asst | 22000 | 26400 |
| 4 | don | it | asst | 26000 | 31200 |
| 5 | joseph | it | asst | 25000 | 30000 |
| 6 | ajay | it | asst | 30000 | 36000 |
+-----+--------+------+---------+--------+------------+
6 rows in set (0.00 sec)

• Create a query to display the eno and ename for all employees
who earn more than average salary. Sort the results in
descending order of salary.

mysql> select eno,ename from emp where salary > (select AVG(salary)from emp)order by
salary DESC;
+-----+--------+
| eno | ename |
+-----+--------+
| 6 | ajay |
| 4 | don |
| 5 | joseph |
+-----+--------+
3 rows in set (0.00 sec)

• Create a view called emp_view based on the eno, ename from


emp table change the heading for the ename to 'EMPLOY'.
mysql> create view emp_view as select eno,ename "EMPLOY" from emp;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from emp_view;


+-----+--------+
| eno | EMPLOY |
+-----+--------+
| 1 | athira |
| 2 | amal |
| 3 | dayana |
| 4 | don |
| 5 | joseph |
| 6 | ajay |
+-----+--------+
6 rows in set (0.01 sec)

• Write a query that will display the eno and ename for all
employees whose name contains a 'T'

mysql> select eno,ename from emp where ename like '%t%';


+-----+--------+
| eno | ename |
+-----+--------+
| 1 | athira |
+-----+--------+
1 row in set (0.01 sec)

5. Create a table department with fields dno, ename, salary,


designation, dname and place with dno as primary key. Insert
values into table
mysql> use office;
Database changed
mysql> create table department(dno int primary key,ename varchar(20),salary
int,designation varchar(20),dname varchar(20),place varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into department


values(1,"vishnu",12000,"manager","accounting","kochi");
Query OK, 1 row affected (0.00 sec)
mysql> insert into department values(2,"hari",10000,"asst.manager","accounting","kannur");
Query OK, 1 row affected (0.00 sec)

mysql> insert into department values(3,"ajay",11000,"accountant","accounting","kannur");


Query OK, 1 row affected (0.00 sec)

mysql> insert into department values(4,"arun",8000,"clerk","accounting","payannur");


Query OK, 1 row affected (0.01 sec)

mysql> select * from department;


+-----+--------+--------+--------------+------------+----------+
| dno | ename | salary | designation | dname | place |
+-----+--------+--------+--------------+------------+----------+
| 1 | vishnu | 12000 | manager | accounting | kochi |
| 2 | hari | 10000 | asst.manager | accounting | kannur |
| 3 | ajay | 11000 | accountant | accounting | kannur |
| 4 | arun | 8000 | clerk | accounting | payannur |
+-----+--------+--------+--------------+------------+----------+
4 rows in set (0.00 sec)

• Write the queries using various Character functions in ename field.


mysql> select concat(ename," ",dname) from department;
+-------------------------+
| concat(ename," ",dname) |
+-------------------------+
| vishnu accounting |
| hari accounting |
| ajay accounting |
| arun accounting |
+-------------------------+
4 rows in set (0.00 sec)

• Create a query to display the employee number and name for all the employee
who earn more than the average salary. Sort the results in descending order of
salary.
mysql> select dno,ename from department where salary>(select avg(salary) from
department) order by salary desc;
+-----+--------+
| dno | ename |
+-----+--------+
| 1 | vishnu |
| 3 | ajay |
+-----+--------+
2 rows in set (0.00 sec)

• Display all employees who got salary between 5000 and 10000.
mysql> select ename from department where salary between 5000 and 10000;
+-------+
| ename |
+-------+
| hari |
| arun |
+-------+
2 rows in set (0.00 sec)

• Display ename, salary and designation for those who got salary more than 5000
or his/her designation =”clerk”.
mysql> select ename,salary,designation from department where salary>5000 or
designation="clerk";
+--------+--------+--------------+
| ename | salary | designation |
+--------+--------+--------------+
| vishnu | 12000 | manager |
| hari | 10000 | asst.manager |
| ajay | 11000 | accountant |
| arun | 8000 | clerk |
+--------+--------+--------------+
4 rows in set (0.00 sec)

• Display ename and designation those who are not a clerk or manager.
mysql> select ename,designation from department where designation !='clerk' and
designation!="manager";
+-------+--------------+
| ename | designation |
+-------+--------------+
| hari | asst.manager |
| ajay | accountant |
+-------+--------------+
2 rows in set (0.00 sec)

• Display the names of all employees where the third letter of their name is an “a”
mysql> select ename from department where substring(ename,3,1)='a';
+-------+
| ename |
+-------+
| ajay |
+-------+
1 row in set (0.00 sec)

You might also like