SQL Interview Questions
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.
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;
from (
from station
)
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.
FROM station
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.
6. Query the list of CITY names from STATION that do not start with vowels. Your result cannot
contain duplicates.
ans:
from station
and
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.
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.
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
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
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 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