Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.
Select city from station where city RLIKE '^[aeiou].*'
Ending in a,e,i,o,u
select distinct city from Station where city RLIKE '.*[aeiou]$'
Start and end in vowels
select distinct city from station where city RLIKE '^[aeiou].*[aeiou]$'
Don’t Start in vowels
select distinct city from station where city RLIKE '^[^aeiou].*'
Don’t end in vowels
select distinct city from station where city RLIKE '.*[^aeiou]$'
Either start OR end in vowels
select distinct city from station where city RLIKE '^[^aeiou].*|[^aeiou]$'
Either start AND end in vowels
select distinct city from station where city RLIKE '^[^aeiou].*[^aeiou]$'
Order by alphabetical order
select name from employee order by name asc
Select Query optimization
● Use select columns instead of select *
● Avoid using distinct clause
● Try creating joins with inner joins and not where
SELECT Customers.CustomerID, Customers.Name, Sales.LastSaleDate
FROM Customers, Sales
WHERE Customers.CustomerID = Sales.CustomerID
SELECT Customers.CustomerID, Customers.Name, Sales.LastSaleDate
FROM Customers
INNER JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
● Use wildcards at the end of a phrase only
SELECT City FROM Customers
○ WHERE City LIKE ‘Char%’
● Use LIMIT to sample query results
Your answer :-
Select count(*), project from EmployeeSalary
Group by project
Having project = ‘P1’
Difference between Using and ON’=
USING
select * from table1_name join table2_name using(column_name);
ON
select * from table1_name from table2_name on table1.coulmn=table2.coulmn;
1. Second highest salary
Select max(salary) from emp_table where salary not in (select max(salary) from
emp_table.
Or
Select max(salary) from emp_table where salary < (select max(salary) from emp_table.
2. Highest salary for all departments
3. Display number of employees in each department
4. Display alternate records in sql
OR
To fetch even Numbered row:
SELECT * FROM table_name WHERE column_name % 2 = 0
To fetch odd Numbered row:
SELECT * FROM table_name WHERE column_name % 2 = 1
5. Display duplicate of column
6. Display employees whose name starts with M.
7. Display employees whose name ends with N.
8. Display names of employees having ‘M’ in any position in their name.
9. Display names of employees whose names do not contain ‘M’ anywhere.
10. Display names of all employees whose name contains exactly 4 letters.
(has 4 underscores)
11. Display names of all employees whose name contains i)second letter as L ii)fourth
letter as M.
.
12. Display emp names and hires dates for emps who joined in the month of December.
Incase date in 1991-11-18 form
https://www.w3resource.com/sql-exercises/employee-database-
exercise/sql-employee-database-exercise-18.php?passed=passe
d
SELECT *
FROM employees
WHERE to_char(hire_date, 'mon')='jan';
13.
14. Display names of all employees whose name contains exactly 2 L’s.
(incorrect for exactly 2 A’s)
%L%L% -> will give atleast 2 L’s
CORRECT ANSWER
15. Display employees whose name starts with J and ends with S.
16. Display nth row of table
Tried Eg :
SELECT * FROM Customers
LIMIT 2 OFFSET 10
(Gives 2 records after 10th row)
For Oracle (using Rownum)
OR
17. If they ask to display all the columns
18. Display 1st 3rnd and 11th record.
SELECT t.*
FROM Customers t
WHERE t.CustomerID IN (1,3, 11)
19. Union and UnionAll
Table 1
Table 2
20. Union all - duplicate values will not be deleted
21. INNER JOINS
TABLE1 - EMP
TABLE 2 -DEPT
22. USING ALIAS
23. Display employees who are working in Location Chicago from Emp and Dept table.
Select* from emp where deptno in (Select deptno from dept where loc = ‘CHICAGO’)
24. Display dept name with total salaries.
25. Self joins
https://www.youtube.com/watch?v=-t6tjGtQ7-8&list=PLqM7alHXFySGweLxxAdBDK
1CcDEgF-Kwx&index=10
26. Display managers of each employee.
27. Display employee details who are getting more salary than their managers.
(if you want to display emp sal add e1.sal and if u want to display manager sal add
e2.sal)
28. Display employee details who joined before their manager.
29. LEFT JOIN
30. Want to add dname and loc columns to emp table
31. Add more conditions:
32. Null values for the rest where dept is not SALES
33. RIGHT JOIN
34. NULL values for those where dept not equal to 20
35. FULL JOIN
36. Dept = 20 for full join
37. CROSS JOIN
(7*4 = 28 rows)
38. Where condition added (chicago only 1 row in dept table hence multiplied with 7
rows of emp table so o/p = 7 rows)
39. Where condition of dept 50
(since dept 50 does not exists)
40. Display first n rows or last n rows
41. SELECT * FROM CUSTOMERS LIMIT 81,10;
42.
43.
44. Fetch last 2 records
SELECT * FROM Customers LIMIT 2 OFFSET (Select count(*)-2 from Customers);
45. Nth highest salary
`
46.
SELECT * FROM (Select DISTINCT * from OrderDetails order by Quantity DESC)
47. 3rd highest salary
48. Intersect
(u need to take same number of
columns from both)(logical ordering position and also data type must be same)
49. MINUS
50. More number of columns
51. 1NF
(1st table wrong)
Tutorial link:
https://www.youtube.com/watch?v=-6v7ctxC7yk&list=PLqM7alHXFySGweLxxAdBD
K1CcDEgF-Kwx
More references :
http://www.mithunashok.com/2011/08/sql-interview-questions-on-functions.html