Friday, June 16, 2017

MySQL query for nth highest salary.

Select name,salary from employee
Order by salary desc limit 1 offset 2.

When set offset 1 then return 2nd, set offset 2 then return 3rd highest salary.

Tuesday, May 2, 2017

What is PDO & why shold we use use PDO?



PDO - PHP Data Objects - is a database access layer providing a uniform method of access to multiple databases.

PHP Data Objects (PDO) provide methods for prepared statements and working with objects that will make you far more productive!

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.  


  • PDO_DBLIB ( FreeTDS / Microsoft SQL Server / Sybase )
  • PDO_FIREBIRD ( Firebird/Interbase 6 )
  • PDO_IBM ( IBM DB2 )
  • PDO_INFORMIX ( IBM Informix Dynamic Server )
  • PDO_MYSQL ( MySQL 3.x/4.x/5.x )
  • PDO_OCI ( Oracle Call Interface )
  • PDO_ODBC ( ODBC v3 (IBM DB2, unixODBC and win32 ODBC) )
  • PDO_PGSQL ( PostgreSQL )
  • PDO_SQLITE ( SQLite 3 and SQLite 2 )
  • PDO_4D ( 4D )


How to connect to a database with both of these:

// PDO
$pdo = new PDO("mysql:host=localhost;dbname=database", 'username', 'password');

// mysqli, procedural way
$mysqli = mysqli_connect('localhost','username','password','database');

// mysqli, object oriented way
$mysqli = new mysqli('localhost','username','password','database');

Friday, April 21, 2017

How to show Record EmployeeName with respective Repoting ManagerName

Let us first create the same table for an employee.
One of the columns in the same table contains the ID of the manger, who is also an employee for the same company. Now all the employees and their managers are present in the same table.

Let us now connect the Employee table with itself with the help of INNER JOIN.
-- Inner Join--














SELECT e1.Employee EmployeeName, e2.Employee AS ManagerName
FROM employee e1
INNER JOIN employee e2 ON e1.Manage_id = e2.ID

-- Self Join --

SELECT A.Employee as EmployeeName , B.Employee as ManagerName
From employee A , employee B WHERE A.Manage_id=B.ID

Result Like That:

What is a Self Join? Explain with Example - Interview Question of the Week #064 selfjoini3






Monday, February 13, 2017

Mongodb Important Questions Answer.

how to install mongoDb in window

http://stackoverflow.com/questions/20796714/what-is-the-way-to-start-mongo-db-from-windows-7-64-bit mongodb setup

where to set mongodbphp.dll file

C:\wamp\bin\php\php5.5.12\ext

link for get the mogodb dll file for php support.

http://www.discussdesk.com/integrate-mongodb-in-codeigniter.htm mongodbphp.ddl

How to fetch record by id in mongodb.

$result=$this->mongo_db->where(array('_id' => new MongoId($id)))->get('users');
return $result;

Note : Here users is a collections.

How to update record in mongodb using where condition by mongodb ojectid?


$result=$this->mongo_db->where((object)array('_id'=>$id))->set($data)->update('adminuser');
return $result;

Note : Here adminuser is a collections.