Very Important and basics SQL Queries for Testing: In the world of software testing, having a strong understanding of SQL queries is essential. SQL (Structured Query Language) is a domain-specific language for managing and manipulating relational databases. SQL queries are used to extract data from a database, and a thorough understanding of SQL syntax and the ability to write efficient queries is crucial for testing database applications.
SQL queries play a significant role in testing data-driven applications. Testers must have the skills to write complex queries to validate data and ensure the application functions as intended. In this context, it is essential for testers to have a solid foundation in SQL queries and be able to apply this knowledge to test and validate database applications effectively.
SQL Queries How To Answer
Display the details of all employees.
Ans: select * from emp;
Display the depart information from the department table.
Ans: select * from dept;
Display the name and job for all the employees.
Ans: select ename, job from emp;
Display the name and salary for all the employees.
Ans: select ename, sal from emp;
Display the employee no and the total salary for all the employees.
Ans: select empno, sal+comm as total from emp group by empno;
Display the employee name and annual salary for all employees.
Ans: select ename, sal * 12 as annual salary from emp;
Display the names of all the employees who are working in depart number 10.
Ans: select ename from emp where deptno=10;
Display the names of all the employees who are working as clerks and drawing a salary of more than 3000.
Ans: select ename from emp where job=’CLERKS ‘and sal>3000;
Display the employee number and name who are earning comm.
Ans: select empno, ename from emp where comm is not null;
Display the employee number and name who do not earn any comm.
Ans: select empno, ename from emp where comm is null;
Display the names of employees who are working as clerks, salesman, or analysts and drawing a salary of more than 3000.
Ans: select ename from emp where job=’CLERK’ OR JOB=’SALESMAN’ OR JOB=’ANALYST’ AND SAL>3000;
Display the names of the employees who are working in the company for the past 5 years.
Ans: select ename from emp where to_char (sysdate,’YYYY’) -to_char (hiredate,’YYYY’) >=5;
Display the list of employees who have joined the company before30-JUN-90 or after 31-DEC-90.
Ans: select ename from emp where hiredate < ’30- JUN-1990′ or hiredate >’31-DEC-90′;
Display current Date.
Ans: select sysdate from dual;
Display the list of all users in your database (use catalog table).
Ans: select username from all users;
Display the names of all tables from the current user.
Ans: select name from tab;
Display the name of the current user.
Ans: show user
SQL Queries Examples
Display the names of employees working in depart number 10 or 20 or 40 or employees working as CLERKS, SALESMAN, or ANALYST.
Ans: select ename from emp where deptno in (10, 20, 40) or job in (‘CLERKS’,’SALESMAN’,’ANALYST’);
Display the names of employees whose name starts with alphabet S.
Ans: select ename from emp where ename like ‘S%’;
Display the Employee names for employees whose name ends with Alphabet S.
Ans: select ename from emp where ename like’%S’;
Display the names of employees whose names have second alphabet A in their names.
Ans: Select ename from EMP where ename like ‘_A%’;
Select the names of the employee whose names are exactly five characters in length.
Ans: select ename from emp where length (ename) =5;
Display the names of the employee who are not working as MANAGERS.
Ans: select ename from emp where job not in (‘MANAGER’);
Display the names of the employee who are not working as SALESMAN OR CLERK OR ANALYST.
Ans: select ename from emp where job not in (‘SALESMAN’,’CLERK’,’ANALYST’);
Display all rows from the EMP table. The system should wait after every screen full of information.
Ans: Set pause on
Display the total number of employees working for the company.
Ans: Select count (*) from EMP;
Display the total salary begging paid to all employees.
Ans: select sum (sal) from emp;
Display the maximum salary from emp table.
Ans: select max (sal) from emp;
Display the minimum salary from emp table.
Ans: select min (sal) from emp;
Display the average salary from emp table.
Ans: select avg (sal) from emp;
Display the maximum salary being paid to the CLERK.
Ans: select max (sal) from emp where job=’CLERK’;
Display the maximum salary being paid to depart number 20.
Ans: select max (sal) from emp where deptno=20;
Display the minimum salary being paid to any SALESMAN.
Ans: select min (sal) from emp where job=’SALESMAN’;
Display the average salary drawn by MANAGERS.
Ans: select avg (sal) from emp where job=’MANAGER’;
Display the total salary drawn by ANALYST working in depart number40.
Ans: select sum (sal) from emp where job=’ANALYST’ and deptno=40;
Display the names of the employee in order of salary, i.e., the name of the employee earning the lowest salary should salary appear first.
Ans: select ename from emp order by sal;
Display the names of the employee in descending order of salary.
Ans: select ename from emp order by sal desc;
Display the names of the employee in order of employee name.
Ans: select ename from emp order by ename;
Display empno, ename, deptno, sal sort the output first base on the name and within name by deptno and within deptno by sal.
Ans: select empno, ename, deptno, sal from emp order by ename, deptno, sal;
Display the name of the employee along with their annual salary (sal*12).T the name of the employee earning the highest annual salary, should appear first.
Ans: select ename, sal*12 from emp order by sal desc;
Display name, salary, hra, pf, da, total salary for each employee. The output should be in the order of total salary; hra 15% of salary, da 10%of salary, pf 5%salary, and total salary will be (salary+hra+da-pf).
Ans: select ename, sal, sal/100*15 as hra, sal/100*5 as pf, sal/100*10 as da, sal+sal/100*15+sal/100*10-sal/100*5 as total from emp;
Display depart numbers and the total number of employees working in each department.
Ans: select deptno, count (deptno) from emp group by deptno;
Display the various jobs and the total number of employees within each job group.
Ans: select job, count (job) from emp group by job;
Display the depart numbers and total salary for each department.
Ans: select deptno, sum (sal) from emp group by deptno;
Display the depart numbers and max salary for each department.
Ans: select deptno, max (sal) from emp group by deptno;
Display the various jobs and total salary for each job.
Ans: select job, sum (sal) from emp group by job;
Display the various jobs and total salary for each job.
Ans: select job, min (sal) from EMP group by job;
Display the depart numbers with more than three employees in each dept.
Ans: select deptno, count (deptno) from emp group by deptno having count (*) >3;
Display the various jobs along with the total salary for each of the jobs where the total salary is greater than 40000.
Ans: select job, sum (sal) from emp group by job having sum (sal) >40000;
Display the various jobs along with the total number of employees in each job. The output should contain only those jobs with more than three employees.
Ans: select job, count (empno) from emp group by job having a count (job) >3
Display the name of the employee who earns the highest salary.
Ans: select ename from emp where sal= (select max (sal) from emp);
Display the employee number and name for an employee working as a clerk and earning the highest salary among clerks.
Ans: select empno, ename from emp where job=’CLERK’ and sal= (select max (sal) from emp where job=’CLERK’);
SQL Queries Basics
Display the names of a salesman who earns the salary more than the highest salary of any clerk.
Ans: select ename, sal from emp where job=’SALESMAN’ and sal> (select max (sal) from emp where job=’CLERK’);
Display the names of clerks who earn a salary more than the lowest salary of any salesman.
Ans: select ename from emp where job=’CLERK’ and sal> (select min (sal) from emp where job=’SALESMAN’);
Display the names of employees who earn a salary more than that of Jones or that of salary greater than that of Scott.
Ans: select ename, sal from emp where sal> (select sal from emp where ename=’JONES’) and sal> (select sal from emp where ename=’SCOTT’);
Display the names of the employees who earn the highest salary in their respective departments.
Ans: select ename, sal, deptno from emp where sal in (select max (sal) from emp group by deptno);
Display the names of the employees who earn the highest salaries in their respective job groups.
Ans: select ename, sal, job from emp where sal in (select max (sal) from emp group by job)
Display the employee names who are working in the accounting department.
Ans: select ename from emp where deptno= (select deptno from dept where dname=’ACCOUNTING’)
Display the employee names who are working in Chicago.
Ans: select ename from emp where deptno= (select deptno from dept where LOC=’CHICAGO’)
Display the Job groups having a total salary greater than the maximum salary for managers.
Ans: SELECT JOB, SUM (SAL) FROM EMP GROUP BY JOB HAVING SUM (SAL) > (SELECT MAX (SAL) FROM EMP WHERE JOB=’MANAGER’);
Display the names of employees from department number 10 with a salary greater than that of any employee working in other departments.
Ans: select ename from emp where deptno=10 and sal>any (select sal from emp where deptno not in 10).
Display the names of the employees from department number 10 with a salary greater than that of all employees working in other departments.
Ans: select ename from emp where deptno=10 and sal>all (select sal from emp where deptno not in 10).
Display the names of the employees in Uppercase.
Ans: select upper (ename) from emp;
Display the names of the employees in Lowercase.
Ans: select lower (ename) from emp;
Display the names of the employees in Proper case.
Ans: select initcap (ename) from emp;
Display the length of your name using an appropriate function.
Ans: select length (‘name’) from dual;
Display the length of all the employee names.
Ans: select length (ename) from emp;
Select the name of the employee to concatenate with the employee number.
Ans: select ename||empno from emp;
User appropriate function and extract 3characters starting from 2characters from the following string ‘Oracle.’ I.e., the output should be ‘ac.’
Ans: select substr (‘oracle’, 3, 2) from dual
find the First occurrence of the character ‘a’ from the following string, i.e., ‘Computer Maintenance Corporation.’
Ans: SELECT INSTR (‘Computer Maintenance Corporation’,’ a’, 1) FROM DUAL
Replace every occurrence of alphabet A with B in the string Allens (use translate function)
Ans: select translate (‘Allens’,’A’,’B’) from dual
Display the information from the emp table. Where the job manager is found, it should be displayed as boos (Use replace function).
Ans: select replace (JOB,’MANAGER’,’BOSS’) FROM EMP;
Display empno, ename, deptno from emp table. Instead of a display, department numbers display the related department name (Use decode function).
Ans: select empno, ename, decode (deptno, 10,’ACCOUNTING’, 20,’RESEARCH’, 30,’SALES’, 40,’OPRATIONS’) from emp;
Display your age in days.
Ans: select to date (sysdate) -to date (’10-Sep-77′) from dual
Display your age in months.
Ans: select months between (sysdate, ’10-Sep-77′) from dual
Display the current date as 15th August Friday Nineteen Ninety Seven.
Ans: select to char (sysdate,’ddth Month day year’) from dual
Scott has joined the company on Wednesday, 13th August, nineteen ninety.
Ans: select ENAME||’ HAS JOINED THE COMPANY ON’||to_char (HIREDATE,’dayddth Month year’) from EMP;
Find the date for the nearest Saturday after the current date.
Ans: SELECT NEXT_DAY (SYSDATE, ‘SATURDAY’) FROM DUAL;
Display the current time.
Ans: select to_char (sysdate,’hh:MM:ss’) from dual.
Display the date three months before the current date.
Ans: select add months (sysdate, 3) from dual;
Display the common jobs from department number 10 and 20.
Ans: select job from emp where deptno=10 and job in (select job from emp where deptno=20);
Display the jobs found in department 10 and 20 Eliminate duplicate jobs.
Ans: select distinct (job) from emp where deptno=10or deptno=20orselect distinct (job) from emp where deptno in (10, 20);
Display the jobs which are unique to department 10.
Ans: select distinct (job) from emp where deptno=10
Display the details of those who do not have any person working under them.
Ans: select e.ename from emp, emp e whereemp.mgr=e.empno group by e.ename having count (*) =1;
Display the details of those employees who are in the sales department and grade is 3.
Ans: select * from emp where deptno= (select deptno from dept where dname=’SALES’) and sal between (select low sal from salgrade where grade=3) and (select hisal from salgrade where grade=3);
Display those who are not managers and who are managers, anyone.
i) Display the managers’ names.
Ans: select distinct (m.ename) from emp e, emp mwhere m.empno=e.mgr;
ii) Display the who are not managers.
Ans: select ename from emp where ename not in (select distinct (m.ename) from emp e, emp m where m.empno=e.mgr);
Display those employees whose name contains not less than 4characters.
Ans: select ename from emp where length (ename) >4;
Display that department whose name starts with “S” while the location name ends with “K”.
Ans: select dname from dept where dname like ‘S%’and loc like ‘%K’;
Display those employees whose manager’s name is JONES.
Ans: select p.ename from emp e, emp p where e.empno=p.mgr and e.ename=’JONES’;
Display those employees whose salary is more than 3000 after giving20% increment.
Ans: select ename, sal from emp where (sal+sal*.2) >3000;
Display all employees while their dept names;
Ans: select ename, dname from emp, dept where emp.deptno=dept.deptno
Display ename who are working in the sales dept.
Ans: select ename from emp where deptno= (select deptno from dept where dname=’SALES’);
Display employee name, deptname, salary, and comm. For those Sal in between2000 to 5000 while the location in Chicago.
Ans: select empno, ename, deptno from emp where deptno= (select deptno from dept where loc=’CHICAGO’) and sal between 2000 and 5000;
Display those employees whose salary greater than his manager’s salary.
Ans: select * from emp e where sal> (select sal from emp where empno=e.mgr);
Display those employees who are working in the same Dept where his manager is working.
Ans: select * from emp e where deptno = (select deptno from emp where empno=e.mgr);
Display those employees who are not working under any manger.
Ans: select * from emp where mgr is null or empno=mgr;
Display grade and employee name for the dept no 10 or 30, but the grade is not 4, while joined the company before 31-Dec-82.
Ans: select empno, ename, sal, deptno, hiredate, grade from emp e, salgrade where e.sal>=s.losal and e.sal<=s.hisal and deptno in (10, 30) andgrade<>4 and hiredate<’01-Dec-1981′;
Update the salary of each employee by 10% increments that are not eligible for commission.
Ans: update emp set sal=sal+ (sal*10/100) where comm is null;
delete those employees who joined the company before 31-Dec-82 while there dept location is ‘NEW YORK’ or ‘CHICAGO.’
Ans: delete from emp where hiredate<’31-Dec-1982′ and deptno in (select deptno from dept where loc in (‘NEW YORK’,’CHICAGO’));
Display employee name, job, deptname, location for all who are working as managers.
Ans: select ename, job, dname, loc from emp e, dept d where e.deptno=d.deptno and empno in (select mgr from emp);
Display those employees whose manager names are Jones, and also display their manager name.
Ans: select e.empno, e.ename, m.ename MANAGER from emp e, emp where e.mgr=m.empno and m.ename=’JONES’;
Display name and salary of ford if his Sal is equal to high Sal of his grade.
Ans: select ename, sal from emp e where ename=’FORD’ and sal=(select hisal from salgrade where grade=(select grade from salgrade where e.sal>=losal and e.sal<=hisal));
Display employee name, his job, his dept name, his manager name, his grade and make out of an under department wise.break on deptno;
Ans: select d.deptno, e.ename, e.job, d.dname, m.ename, s.grade from emp e, emp m, Dept d, salgrade s where e.deptno=d.deptno and e.salbetween s.losal and s.hisal and e.mgr=m.empno order by e.deptno;
List out all the employee’s name, job, and salary grade and department name for everyone in the company except ‘CLERK’. Sort on salary displays the highest salary.
Ans: select empno, ename, sal, dname, grade from emp e, dept d, salgrade where e.deptno=d.deptno and e.sal betweens.losal and s.hisal ande.job<>’CLERK’ order by sal;
Display employee name, his job, and his manager. Display also employees’ who are without the manager.
Ans: select e.ename, e.job, m.ename Manager from emp e,emp m where.mgr=m.empno union select ename, job,’ no manager’ from emp where mgr is null;
SQL Queries Basics
Find out the top 5 earners of the company.
Ans: select * from emp e where 5> (select count (*) from emp wheresal>e.sal) order by sal desc;
Display the name of those employees who are getting the highest salary.
Ans: select empno, ename, sal from emp where sal=(select max(sal) from emp);
Display those employees whose salary is equal to an average of maximum and minimum.
Ans: select * from emp where sal= (select (max (sal) +min (sal))/2 fromemp);
Display count of employees in each department where count greater than 3.
Ans: select deptno, count (*) from emp group by deptno having count (*)>3;
Display dname where at least 3 are working and display the only dname.
Ans: select dname from dept where deptno in (select deptno from emp group by deptno having count (*)>3);
Display the name of those managers name whose salary is more than the average salary of a company.
Ans: select ename, sal from emp where empno in (select mgr from emp) and sal > (select avg (sal) from emp);
Display those manager’s names whose salary is more than the average salary of his employees.
Ans: select ename, sal from emp e where empno in (select mgr from emp) and e.sal> (select avg (sal) from emp where mgr=e.empno);
Display employee name, Sal, commission, and net pay for those employees whose net pay are greater than or equal to any other employee salary of the company?
Ans: select ename, sal, comm, sal+nvl (comm, 0) netPay from emp wheresal+nvl (comm., 0)>=any (select sal from emp);
Display those employees whose salary is less than his manager but more than the salary of any other managers.
Ans: select * from emp e where sal<(select sal from emp where empno =e.mgr) and sal>any(select sal from emp where empno!=e.mgr);
Display all employee’s names with total Sal of a company with each employee name.
Ans: Select ename, (select sum (sal) from emp) from emp;
Find out the last 5(least) earner of the company?
Ans: select * from emp e where 5> (select count (*) from emp wheresal<e.sal) order by sal;
Find out the number of employees whose salary is greater than there manager salary?
Ans: select count (*) from emp e where sal> (select sal from emp whereempno=e.mgr);
Display that manager who is not working under the president, but they are working under any other manager?
Ans: select * from emp e where mgr in (select empno from emp whereename<> ‘KING’);
Delete those departments where is no employee working?
Ans: delete from dept d where 0= (select count (*) from emp wheredeptno=d.deptno);
Delete those records from the EMP table whose deptno not available in the dept table?
Ans: delete from emp where deptno not in (select deptno from dept);
Display those earners whose salary is out of the grade available in the Sal grade table?
Ans: select * from emp where sal< (select min (losal) from salgrade) orsal> (select max (hisal) from salgrade);
Display employee name, Sal, commission. and whose net pay is greater than any other in the company?
Ans: select ename, sal, comm from emp where sal+sal*15/100-sal*5/100+sal*10/100 = (select max (sal+sal*15/100-sal*5/100+sal*10/100) from emp);
Display name of those employees who are going to retire 31-Dec-99. If the maximum job is period is 18 years?
Ans: select * from emp where (to_date (’31-dec-1999′)-hiredate)/365>18;
Display those employees whose salary is ODD value?
Ans: select * from emp where mod (sal, 2) =1;
Display those employees whose salary contains at least 4 digits?
Ans: select * from emp where length (sal)>=4;
Display those employees who joined the company in the month of DEC?
Ans: select * from emp where upper (to_char (hiredate,’mon’)) =’DEC’;
Display those employees whose name contains “A”?
Ans: select * from emp where instr (ename,’A’, 1, 1)>0;
Display those employees whose deptno is available in salary?
Ans: select * from emp where instr (sal, deptno, 1, 1)>0;
Display those employees whose first 2 characters from hire date-last 2characters of salary?
Ans: select substr(hiredate,0,2)||substr(sal,length(sal)-1,2) from emp; select concat( substr(hiredate,0,2), substr(sal,length(sal)-1,2) ) fromemp;
Display those employees whose 10% of salary is equal to the year of joining?
Ans: select * from emp where to_char (hiredate,yy’) =sal*10/100;
Display those employees who are working in sales or research?
Ans: select * from emp where deptno in (select deptno from dept where name in (‘SALES’,’RESEARCH’));
Display the grade of Jones?
Ans: select grade from salgrade where losal<= (select (sal) from emp whereename=’JONES’) and hisal>= (select (sal) from emp whereename=’JONES’);
Display those employees who joined the company before the 15Th of the month?
Ans: select empno, ename from emp where hiredate< (to_date (’15-‘||to_char(hiredate,’mon’)||’-‘||to_char(hiredate,’yyyy’)));
Delete those records where no of employee in a particular department is lessthan 3?
Ans: delete from emp where deptno in (select deptno from emp group by deptno having count (*)>3);
Delete those employees who joined the company 21 years back from today?
Ans: select * from emp where round ((sysdate-hiredate)/365)>21;
Or
Ans: select * from emp where (to_char (sysdate, ‘yyyy’)-to_char (hiredate,’yyyy’))>21;
Display the department name the no of characters of which is equal to no of employees in any other department?
Ans: select dname from dept where length (dname) in (select count (*) from emp group by deptno);
Display those employees who are working as a manager?
Ans: select * from emp where empno in (select mgr from emp);
Count the no of employees who are working as a manager (use set operation)?
Ans: select count (*) from emp where empno in (select mgr from emp);
Display the name of then dept those employees who joined the company on the same date?
Ans: select empno, ename, hiredate, deptno from emp e where hiredate in (select hiredate from emp where empno<>e.empno);
Display the manager who is having a maximum number of employees working under him?
Ans: select mgr from emp group by mgr having count (*) =(select max(count(mgr)) from emp group by mgr);
List out employee’s name, and salary increased by 15% and expressed as the whole number of dollars?
Ans: select empno, ename, lpad (concat (‘$’, round (sal*115/100)), 7) salary from emp;
Produce the output of the EMP table “EMPLOYEE_AND_JOB” forename and job?
Ans: select * from EMPLOYEE_AND_JOB;
List all employees with hire date in the format ‘June 4, 1988’?
Ans: select to_char (hiredate,’ month dd yyyy’) from emp;
SQL Queries Examples With Answers
Print a list of employees displaying ‘Less Salary’ if less than 1500 if exactly1500 display as ‘Exact Salary’ and if greater than 1500 display ‘More Salary’?
Ans: select empno, ename,’Less Salary ‘||sal from emp where sal<1500union select empno, ename,’More Salary ‘||sal from emp where sal>1500union select empno, ename,’Exact Salary ‘||sal from emp where sal=1500
Write a query to calculate the length of the employee who has been with the company?
Ans: select round (sysdate-hiredate) from emp;
Display those managers who are getting less than their employees Sal.
Ans: select empno from emp e where sal<any (select sal from emp wheremgr=e.empno);
Print the details of all the employees who are subordinate to Blake.
Ans: select * from emp where mgr= (select empno from emp whereename=’BLAKE’);
Display those who work as a manager using a co-related subquery.
Ans: select * from emp where empno in (select mgr from emp);
Display those employees whose manager’s name is Jones and also with his manager’s name.
Ans: select * from emp where mgr= (select empno from emp whereename=’JONES’) union select * from emp where empno= (select mgrfrom emp where ename=’JONES’);
Use the variable in a statement which finds all employees who can earn 30,000a year or more.
Ans: select * from emp where &emp_ann_sal>30000;
Find out how many managers are there without listing them.
Ans: select count (*) from EMP where empno in (select mgr from EMP);
Find out the avg sal and avg total remuneration for each job type remember salesmen earn the commission.
Ans: select job, avg (sal+nvl (comm, 0)), sum (sal+nvl (comm, 0)) from empgroup by job;
Check whether all employees number are indeed unique.
Ans: select count(empno),count(distinct(empno)) from emp havingcount(empno)=(count(distinct(empno)));
List out the lowest paid employees working for each manager, exclude anygroups where min sal is less than 1000 sort the output by sal.
Ans: select e.ename, e.mgr, e.sal from emp e where sal in (select min (sal) from emp where mgr=e.mgr) and e.sal>1000 order by sal;
list ename, job, annual salary, deptno, dname, and grade who earn 30000 per year and who are not clerks.
Ans: select e.ename, e.job, (e.sal+nvl (e.comm,0))*12, e.deptno, d.dname,s.grade from emp e, salgrade s , dept d where e.sal between s.losaland s.hisal and e.deptno=d.deptno and (e.sal+nvl(comm,0))*12>30000 and e.job <> ‘CLERK’;
find out the all employees who joined the company before their manager.
Ans: select * from emp e where hiredate< (select hiredate from emp whereempno=e.mgr);
list out all employees by name and number along with their manager’s name and number also display ‘No Manager’ who has no manager.
Ans: select e.empno, e.ename, m.empno Manager, m.ename ManagerNamefrom emp e, emp m where e.mgr=m.emp union select empno, ename, mgr,’No Manager’ from emp where mgr is null;
find out the employees who earned the highest Sal in each job typed sort in descending Sal order.
Ans: select * from emp e where sal = (select max (sal) from emp where job=e.job);
find out the employees who earned the min Sal for their job in ascending order.
Ans: select * from emp e where sal = (select min (sal) from emp where job=e.job) order by sal;
find out the most recently hired employees in each dept order by hire date.
Ans: select * from emp order by deptno, hiredate desc;
display ename, sal, and deptno for each employee who earns a Sal greater than the avg of their department order by deptno.
Ans: select ename, sal, deptno from emp e where sal> (select avg (sal) from emp where deptno=e.deptno) order by deptno;
SQL Queries Online
display the department where there are no employees.
Ans: select deptno, dname from dept where deptno not in (select distinct(deptno) from emp);
Display the dept no with the highest annual remuneration bill as compensation.
Ans: select deptno, sum (sal) from emp group by deptno having sum (sal) =(select max(sum(sal)) from emp group by deptno);
In which year did most people join the company. Display the year and number of employees.
Ans: select count (*), to_char (hiredate,’yyyy’) from emp group byto_char (hiredate,’yyyy’);
display avg sal figure for the dept.
Ans: select deptno, avg (sal) from emp group by deptno;
Write a query of display against the row of the most recently hired employee. Display ename hire date and column max date showing.
Ans: select empno, hiredate from emp where hiredate= (select max (hiredate) from emp);
display employees who can earn more than the lowest Sal in dept no 30.
Ans: select * from emp where sal> (select min (sal) from emp wheredeptno=30);
find employees who can earn more than every employee in dept no 30.
Ans: select * from emp where sal> (select max (sal) from emp wheredeptno=30); select * from emp where sal>all (select sal from emp wheredeptno=30);
select dept name dept no and the sum of Sal break on deptno on dname.
Ans: select e.deptno, d.dname, sal from emp e, dept d where e.deptno=d.deptno order by e.deptno;
find all dept’s which have more than 3 employees.
Ans: select deptno from emp group by deptno having count (*)>3;
Display half of the enames in the upper case and the remaining lower case.
Ans: select concat ( upper ( substr ( ename, 0 , length (ename)/ 2) ),lower (substr (ename, length(ename) / 2+1, length(ename) )) ) from emp;
Select ename if ename exists more than once.
Ans: select distinct (ename) from emp e where ename in (select ename from emp where e.empno<>empno);
display all enames in reverse order.
Ans: select ename from emp order by ename desc;
Display that employee whose joining of month and grade is equal.
Ans: select empno, ename from emp e, salgrade s where e.sal betweens.losal and s.hisal and to_char (hiredate,’ mm’) =grade;
Display that employee whose joining date is available in dept no.
Ans: select * from emp where to_char (hiredate,’dd’) =deptno;
Display those employee’s names as follows A ALLEN, B BLAKE.
Ans: select substr (ename, 1, 1) ||’ ‘||ename from emp;
List out the employees ename, sal, PF from emp.
Ans: select ename, sal, sal*15/100 PF from emp;
Create table emp with only one column empno.
Ans: Create table emp (empno number (5));
Add this column to emp table ename Varchar (20).
Ans: alter table emp add ename varchar2 (20) not null;
OOPS! I forgot to give the primary key constraint. Add it now.
Ans: alter table emp add constraint emp_empno primary key (empno);
now increase the length of the ename column to 30 characters.
Ans: alter table emp modify ename varchar2 (30);
Add salary column to emp table.
Ans: alter table emp add sal number (7, 2);
I want to give a validation saying that sal cannot be greater 10,000(note provide a name for this column).
Ans: alter table emp add constraint emp_sal_check check (sal<10000);
For the time being, I have decided that I will not impose this validation. My boss has agreed to pay more than 10,000.
Ans: Alter table emp disable constraint emp_sal_check;
My boss has changed his mind. Now he doesn’t want to pay more than10, 000. So revoke that salary constraint
Ans: Alter table emp enable constraint emp_sal_check;
Add a column called mgr to your emp table.
Ans: Alter table emp add mgr number (5);
Oh! This column should be related to the empno. Give a command to add this constraint.
Ans: Alter table emp add constraint emp_mgr foreign key (empno);
Add dept no column to your emp table.
Ans: Alter table emp add deptno number (3);
This dept no column should be related to a deptno column of the dept table.
Ans: Alter table emp1 add constraint emp1_deptno foreign key (deptno) references dept (deptno);
Create a table called newemp. Using a single command creates this table as well as to get data into this table (use create table as).
Ans: create table newemp as select *from emp;
Create a table called newemp. This table should contain only empno, ename, dname.
Ans: create table newemp as select empno, ename, dname from emp e, dept d where e.deptno=d.deptno;
Delete the rows of employees who are working in the company for more than 2years.
Ans: Delete from emp where floor (sysdate-hiredate)>2*365;
Provide a commission to employees who are not earning any commission.
Ans: update emp set comm=300 where comm is null;
If any employee has commissioned his commission should be incremented by10% of his salary.
Ans: update emp set comm=comm*10/100 where comm is not null;
Display employee name and department name for each employee.
Ans: select ename, dname from emp e, dept d where e.deptno=d.deptno;
Display employee number, name, and location of the department in which he is working.
Ans: select empno, ename, loc from emp e, dept d where e.deptno=d.deptno;
Display ename, dname even if there no employees working in a particular department (use outer join).
Ans: select ename, dname from emp e, dept d where e.deptno (+) =d.deptno;
Display employee name and his manager’s name.
Ans: select e.ename, m.ename from emp e, emp m where e.mgr=m.empno;
Display the department name along with the total salary in each department.
Ans: select deptno, sum (sal) from emp group by deptno;
Display the department name and the total number of employees in each department.
Ans: select deptno, count (*) from emp group by deptno;
Display the current date and time.
Ans: select to_char (sysdate,’month mon dd yy yyyy hh: mm: ss’) from dual;
Conclusion:
A strong understanding of SQL queries is a critical skill for software testers who are responsible for testing data-driven applications. SQL queries are essential for retrieving and validating data from relational databases, and testers must have the skills to write complex queries to ensure that the application is functioning as intended. Testing database applications requires a thorough knowledge of SQL syntax, the ability to write efficient and optimized queries, and an understanding of relational database concepts.
By mastering SQL queries, testers can validate data accurately and effectively, and ensure that the application meets the required quality standards. In today’s fast-paced and data-driven world, the importance of SQL queries in software testing cannot be overstated, and it is crucial for testers to invest time and effort in acquiring and honing this skill.
Reference: ArticleSQL
Share me data
Hi team
Pleaae provide the data above questions
provide data please
Hi ,
Please provide above data
32) Display all the employees who are getting some commission in marketing department where the employees have joined only on weekdays.
Thanks For detailed article