MySQL

Most Essential And Frequently Asked Interview Questions And Answer

Ans:

OPTIMIZE command is use for optimize a table.

 OPTIMIZE TABLE `users` 

 

Ans:

SQL | difference between BETWEEN and IN operator

The BETWEEN operator select all records between the given range whereas IN operator select all matching records  given with WHERE clause.

View Detials
Ans:

For increaseing the speed of a query we have to do couple of things

  • First of all instead of using select * from table1, use select column1, column2, column3.. from table1.
  • Try to introduce index in the table.
  • Always use limit keyword if we are looking for any specific number of rows from the result set.
Ans:

SQL | View

View in SQL is a kind of virtual table. It does not physical exists in our database but have rows and column as there we have in real database table. we can create a view by selecting fields from one or tables present in the database. A View can have all the rows of a table or specific rows based on certain condition.

View Detials
Ans:

DATEDIFF function is use for finding the number of days between two given dates using MySQL

 DATEDIFF(‘2018-03-07′,’2017-01-01’)

 

Ans:

Bellow code is use for the same

 $sql = "SELECT * FROM table1";
$result = mysql_query($sql, $db_link);
$num_rows = mysql_num_rows($result);
echo $num_rows;

 

Ans:

We can use bellow query to count number of rows in a table

 >SELECT COUNT(*) FROM table_name;

 

Ans:

SQL | LIKE operator

The LIKE operator of SQL is used for such kind of operations. It is used to fetch filtered data by searching for a particular pattern in where clause.

View Detials
Ans:

The three different way in PHP to connect with MySQL is:

  1. MySQL
  2. MySQLI
  3. PDO

Note: We should use MySQLI because MySQLI is imporoved version of MySQL and have more function then MySQL.

Ans:

SQL | Difference between primary key and unique constraints

There is only one primary key in a table. It creates the cluster index automatically and it cannot have NULL value
where as there can be multiple unique constraints in a table. It dose not creates the cluster index automatically and it can have NULL value

Ans:

Mostly we use:

  • utf8_general_ci
  • utf8_unicode_ci

utf8_general_ci is use for fast sorting but it is less accurate whereas utf8_unicode_ci is used for better accuracy but little slow as compare to utf8_general_ci.

We can also use other collation based on specific languages like utf8_swedish_ci.

Ans:

There are 5 different types of storage engine present in MySql.

  1. MyISAM
  2. Heap
  3. Merge
  4. INNO DB
  5. ISAM

MyISAM is the default storage engine for MySQL

Ans:

By using mysql function mysqli_insert_id() we can retrive the last inserted id in database table.

Ans:

Trigger

Trigger can be define as a database object just like a stored procedure. It is a set of actions which fires automatically when an event occure in a database.

View Detials
Ans:

By using following query we can find out the nth largest salary from employees table.

 SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET n-1

In the above query n is replaced by any number where n >= 1. Suppose you want to find fifth highest salary then you have to use offset = 5-1 i.e.: 4

The query will be:

 SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET 4