Some Python SQL Queries
Some Python SQL Queries
•1.Create
and open Database named MYORG.
Ans: Create database MYORG;
6.Add one column State of data type VARCHAR and size 30 to table DEPT
Ans: alter table DEPT
Add(state varchar(30));
7.Create a table name EMP with following structure
EMP
12.Write a query to display the name of employee whose name contains “A” as third
alphabet in Ascending order of employee names.
Ans: select empname
From emp
Where empname like “__A%”
Order by empname;
13.Display the sum of salary and commission of employees as “Total Incentive” who
are getting commission .
Ans: select sal+comm As “Total Incentive”
From emp
where comm is not NULL;
14.Show the average salary for all departments with more than 5 working people.
Ans: select avg(sal)
From emp
Group by deptid
Having count(*)>5;
15.Display the distinct designation offered by the Organization.
Ans: select distinct designation
From emp;
16.List the count of Employees grouped by DeptID.
Ans: select count(*)
From emp
Group by DeptID;
17.Display the names of employees who joined on or after 01/05/1991.
Ans: Select empname
From emp
Where DOJ>=’01/05/1991’;
18.Display the employee records in order by DOJ.
Ans: select *
From emp
Order by DOJ;
19.Display the maximum salary of employees in each Department.
Ans: select max(sal)
From emp
Group by department;
20.Update all the records as add ‘Mr.’ with EmpName.
Ans: update emp
Set EmpName=concat(‘Mr’,EmpName);
21.Display the name of Employees who is working in SALES department.
Ans: select empname
From emp, dept
Where deptName=”SALES” and emp.DeptID=dept.DeptID;
22.Drop the emp table.
Ans: drop table emp;
23.Delete all the records who is working as “CLERK”
Ans: delete from emp
Where designation=”CLERK”;
24.Show the minimum, maximum and average salary of Managers.
Ans: select min(sal), max(sal), avg(sal)
From emp
Where designation=”Manager”;
25.Increase the salary of managers by 15%.
Ans: update emp
Set sal= sal + 0.15*sal
Where designation=”Manager”;
26.To display the name of those employees whose location is Delhi.
Ans: select empname
From emp, dept
Where location=”Delhi” and emp.DeptID=dept.DeptID;
27.To display total salary of employees of sales department.
Ans: select sum(sal)
From emp, dept
Where deptName=”SALES” and emp.DeptID=dept.DeptID;
28.To show details of employees who joined before ‘01/04//1995’.
Ans: select *
From emp
Where DOJ<’01/04/1995’;
29.Delete all the records who is working as “SALESMAN” and salary more than 1500.
Ans: delete from emp
Where designation=” SALESMAN” and sal>1500;
30.Set the commission as 100 who are not getting any commission.
Ans: update emp
Set comm= 100
Where comm Is NULL;