SQL
SQL
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.
• 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.
• 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)
• 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 ename for those who are getting salary in between 5000
and 10000.
• Create a view named ‘star’ with fields ename ,salary & place.
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)
• 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)
• 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)
• Write a query that will display the eno and ename for all
employees whose name contains a 'T'
• 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)