SQL interview questions:
1. Let be the number of CITY entries in STATION, and let be the number of distinct CITY names
in STATION; query the value of from STATION. In other words, find the difference between the total
number of CITY entries in the table and the number of distinct CITY entries in the table.
where LAT_N is the northern latitude and LONG_W is the western longitude.
Ans: SELECT (COUNT(CITY)- COUNT(DISTINCT(CITY))) AS diff FROM STATION;
2. Query the two cities in STATION with the shortest and longest CITY names, as well as their
respective lengths (i.e.: number of characters in the name). If there is more than one smallest or
largest city, choose the one that comes first when ordered alphabetically.
Ans 1: select * from(select distinct city,length(city) from station order by length(city) asc,city asc)
where rownum=1
union
select * from(select distinct city,length(city) from station order by length(city) desc,city desc) where
rownum=1;
ans2: select min(city), len
from (
select city, length(city) len,
max(length(city)) over() maxlen,
min(length(city)) over() minlen
from station
)
where len in(minlen,maxlen)
group by len;
3. Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your
result cannot contain duplicates.
ans: select distinct city from station where SUBSTR(city,1,1) IN('A','E','I','O','U') Order by city;
4. Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION.
ans: SELECT DISTINCT city
FROM station
WHERE SUBSTR(lower(CITY),-1) IN ('a','e','i','o','u');
5. I want to query the list of CITY names from the table STATION(id, city, longitude,
latitude) which have vowels as both their first and last characters.
ans1: SELECT DISTINCT city
FROM station
WHERE SUBSTR(lower(CITY),1,1) IN ('a','e','i','o','u') AND SUBSTR(lower(CITY),-1) IN ('a','e','i','o','u');
6. Query the list of CITY names from STATION that do not start with vowels. Your result cannot
contain duplicates.
ans:
select distinct city
from station
where not lower(substr(city,1,1)) in ('a','e','i','o','u')
or not lower(substr(city,-1,1)) in ('a','e','i','o','u');
7. Query the list of CITY names from STATION that do not start with vowels and do not end with
vowels. Your result cannot contain duplicates.
ans: select distinct city from station
where (lower(city) not like 'a%'
and lower(city) not like 'e%'
and lower(city) not like 'i%'
and lower(city) not like 'o%'
and lower(city) not like 'u%')
and
(lower(city) not like '%a'
and lower(city) not like '%e'
and lower(city) not like '%i'
and lower(city) not like '%o'
and lower(city) not like '%u');
8. Query the Name of any student in STUDENTS who scored higher than Marks. Order your output
by the last three characters of each name. If two or more students both have names ending in the
same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
ans: select name from students where marks > 75 order by substr(name, -3), id;
9. Query the smallest Northern Latitude (LAT_N) from STATION that is greater than . Round your
answer to decimal places.
ans: SELECT ROUND(MIN(LAT_N),4) FROM STATION WHERE LAT_N > 38.7780;
10. Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N)
in STATION is greater than . Round your answer to decimal places.
ans: SELECT ROUND(LONG_W,4) FROM STATION
WHERE LAT_N = ( SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);
11. Consider P1(a,b) and P2(c,d) to be two points on a 2D plane.
a happens to equal the minimum value in Northern Latitude
(LAT_N in STATION).
b happens to equal the minimum value in Western Longitude
(LONG_W in STATION).
c happens to equal the maximum value in Northern Latitude
(LAT_N in STATION).
d happens to equal the maximum value in Western Longitude
(LONG_W in STATION).
Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4
decimal places.
Input Format
The STATION table is described as follows:
STATION
Field Type
ID NUMBER
CITY VARCHAR2(21)
STATE VARCHAR2(2)
LAT_N NUMBER
LONG_W NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
Analysis
The Manhattan Distance is |x1 - x2| + |y1 - y2| = |a - c| + |b - d|.
|a - c| + |b - d| ==> ABS(MIN(LAT_N)-MAX(LAT_N)) + ABS(MIN(LONG_W)-
MAX(LONG_W))
round to a scale of 4 decimal places ==> SELECT ROUND(ABS(MIN(LAT_N)-
MAX(LAT_N)) + ABS(MIN(LONG_W)-MAX(LONG_W)), 4)
from STATION table ==> FROM STATION
ans: SELECT ROUND(ABS(MIN(LAT_N)-MAX(LAT_N)) + ABS(MIN(LONG_W)-MAX(LONG_W)), 4) FROM
STATION;
Consider P1(a,c) and P2(b,d) to be two points on a 2D plane where (a,b) are the
respective minimum and maximum values of Northern Latitude (LAT_N) and (c,d) are
the respective minimum and maximum values of Western Longitude
(LONG_W) in STATION.
Query the Euclidean Distance between points P1 and P2 and format your answer to
display 4 decimal digits.
Input Format
The STATION table is described as follows:
STATION
Field Type
ID NUMBER
CITY VARCHAR2(21)
STATE VARCHAR2(2)
Field Type
LAT_N NUMBER
LONG_W NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
Analysis
The Euclidean Distance is sqrt((a-b)2 + (c-d)2)
Euclidean Distance ==> SQRT(POW(MIN(LAT_N)-MAX(LAT_N), 2) +
POW(MIN(LONG_W)-MAX(LONG_W), 2))
round answer to 4 decimal places ==> SELECT
ROUND(SQRT(POW(MIN(LAT_N)-MAX(LAT_N), 2) + POW(MIN(LONG_W)-
MAX(LONG_W), 2)), 4)
in STATION table ==> FROM STATION
ans: SELECT ROUND(SQRT(POWER(MIN(LAT_N)-MAX(LAT_N), 2) + POWER(MIN(LONG_W)-
MAX(LONG_W), 2)), 4) FROM STATION;
12. 3rd Highest salary
create or replace procedure hi_sal(sal number:=3) is sal12 number; begin select distinct salary into sal12 from
employees e1 where 3 = (select count(distinct salary) from employees e2 where e1.salary<=e2.salary);
dbms_output.put_line(sal12); end; /
CREATE OR REPLACE PROCEDURE n_high_salary (n NUMBER) IS CURSOR cur_salary IS SELECT
salary FROM employee ORDER BY salary DESC; v_salary cur_salary%ROWTYPE; counter NUMBER(10)
:= 1; BEGIN FOR v_salary IN cur_salary LOOP IF counter = n THEN DBMS_OUTPUT.PUT_LINE('The
highest '||n||' salary is '||v_salary.salary); EXIT; END IF; counter := counter + 1; END LOOP; END
n_high_salary; /
create or replace procedure pr_third_sal (var_third number) is var_salary employees.salary%type; begin
SELECT salary INTO var_salary FROM employees ORDER BY salary desc OFFSET 2 ROWS FETCH
NEXT 1 ROWS ONLY; dbms_output.put_line('Third Highest salaray of the employees is:'||var_salary); END;
/ EXEC pr_third_sal(1);
CREATE OR REPLACE PROCEDURE nm_highest_sal (nm_of_salary NUMBER) IS v_salary number:=0;
BEGIN SELECT salary INTO v_salary FROM (SELECT salary, ROWNUM as RN FROM (SELECT
DISTINCT employees.salary FROM employees ORDER BY 1 DESC)) WHERE RN = nm_of_salary;
DBMS_OUTPUT.PUT_LINE(v_salary ||' is the ' || nm_of_salary ||' highest salary.'); END; /
create or replace procedure nth_highest_sal(which_sal number) is var_nth_sal employees.salary%type; begin
select distinct salary into var_nth_sal from employees a where which_sal = (select count(distinct salary) from
employees b where b.salary >= a.salary) order by a.salary desc; dbms_output.put_line(which_sal||'th salary of
employee is '||var_nth_sal); end; / begin nth_highest_sal(3); end; /
CREATE OR REPLACE PROCEDURE THIRD_HIGH_SAL AS CURSOR THIRD IS SELECT * FROM
EMP A WHERE 3= (SELECT COUNT(DISTINCT SAL) FROM EMP B WHERE A.SAL <= B.SAL);
BEGIN FOR I IN THIRD LOOP DBMS_OUTPUT.PUT_LINE('EMP 3RD HIGEST SAL IS::'||I.SAL); END
LOOP; END THIRD_HIGH_SAL;
CREATE OR REPLACE PROCEDURE PP IS CURSOR THIRD IS SELECT * FROM EMPLOYEES
WHERE SALARY=(SELECT MAX(SALARY) FROM EMPLOYEES WHERE SALARY<(SELECT
MAX(SALARY) FROM EMPLOYEES WHERE SALARY<(SELECT MAX(SALARY) FROM
EMPLOYEES))); BEGIN FOR I IN THIRD LOOP DBMS_OUTPUT.PUT_LINE('THE 3RD SALARY
IS:'||I.SALARY); END LOOP; END;
Create or REPLACE PROCEDURE third_hig_Salary(sal out Number) IS Begin Select max(salary) INTO sal
from employee WHERE salary<(Select MAX(salary) FROM employee where salary<(select max(salary) from
employee)); DBMS_OUTPUT.put_line('Third Highest Salary is '||sal); ENd third_hig_Salary; / VARIABLE
sala Number; EXECUTE third_hig_salary(:sala);
create or replace procedure proc_1 is e_name varchar2(20); salary number; begin select max(sal) into salary
from emp22 where sal < (select max(sal) from emp22 where sal < (select max(sal) from emp22)); select ename
into e_name from emp22 where sal = salary; dbms_output.put_line( 'Third highest salary is ' || salary ||' drawn
by '|| e_name ); end; / set serveroutput on; execute proc_1;
Create Or Replace PROCEDURE High3sal Is VSAL EMP.SAL%TYPE; Begin Select Sal Into Vsal From
Emp Order By Sal Desc Offset 3 Rows Fetch NEXT 1 Row Only; DBMS_OUTPUT.PUT_LINE(VSAL);
End; EXECUTE HIGH3SAL
create or replace procedure proc_third_high_Sal(var_rank Number) is var_emp_id
employees.employee_id%type; var_salary NUMBER; Begin select employee_id, salary into var_emp_id,
var_salary from ( select employee_id,salary, dense_rank() over (order by salary desc) as sal_rank from
employees ) where sal_rank = var_rank; dbms_output.put_line('Third Highest salaray of the employees
is:'||var_salary); End; /