Top 12 SQL Interview Questions



1) How to create database connection and query in PHP?
mysqli_query()
PDO::_query()

2) Get department wise average salary from employee table order by salary ascending
select dept,avg(SALARY) AvgSalary from employee group by dept order by AvgSalary asc

3) Get department wise maximum salary from employee table order by salary ascending
select dept,max(SALARY) MaxSalary from employee group by dept order by MaxSalary asc

4) Get department wise maximum salary from employee table order by salary ascending
select dept,max(SALARY) MaxSalary from employee group by dept order by MaxSalary asc

5) Get employee details from employee table whose Salary greater than 600000
SELECT * FROM `employee` WHERE Salary >60000

6) Get employee details from employee table whose first name starts with 'h'
Select * from j2 where name like 'h%'

7) Get employee details from employee table whose first name contains 'h'
Select * from j2 where name like '%h%'

8) Get all employee details from the employee table order by First_Name Ascending
Select * from j2 order by name asc

10) find out second highet salary

-> select name,salary from employee order by salary desc limit 1 OFFSET 1(second highest salary)
select name,salary from employee order by salary desc limit 1 OFFSET 2(third highest salary))

11) Difference between Primary Key and Foreign Key
- Primary key
Primary key uniquely identify a record in the table.
Primary Key can't accept null values. 
We can have only one Primary key in a table. 


-Foreign key
Foreign key is a field in the table that is primary key in another table.
Foreign key can accept multiple null value.
We can have more than one foreign key in a table.


12) what is index

Indexes allow the database application to find data fast; without reading the whole table
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries.

- types of index

- Clustered,Nonclustered,Unique,Full-text,Filtered,XML

Post a Comment

0 Comments