Category: MySQL

How to kill a MySQL query?

February 3, 2010 | Filed Under MySQL, PHP | Leave a Comment

If you fired a query, that takes more time to complete their execution.
but you want to stop this query.
To stop this query you must use Kill command to achieve it.
Follow two step to kill your query.
mysql> SHOW PROCESSLIST;
above command will show all running process on the server.
Here you will see thread ID return by above command.
mysql> KILL 22;
kill thread 22 to kill your query.

Article written by admin

Differences between DROP and TRUNCATE a table?

January 15, 2010 | Filed Under MySQL | Leave a Comment

DROP TABLE table_name – This will delete the table and its data.

TRUNCATE TABLE table_name – This will delete the data of the table, but not the table definition.

Article written by admin

What is the default table in MYSQL? which type of table is generatedby default.

January 10, 2010 | Filed Under MySQL | Leave a Comment

MyISAM is the default storage engine.

Article written by admin

How many number of tables are present in MYSQL Data base?

January 10, 2010 | Filed Under MySQL, PHP | Leave a Comment

Total 5 types of tables we can create
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM

Article written by admin

what is LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN in mysql?

January 10, 2010 | Filed Under MySQL, PHP | Leave a Comment

The best way to learn about JOINS is the example. So below is the example to demonstrate JOINS
Suppose that there are two tables
1- Person
2-Information
Person table has definition and data like that
Person:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

Information:

Entry_id Number Address
1 1 Chand Pura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

mysql> select * from person;

out put:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

3 rows in set (0.00 sec)

mysql> select * from information;

Entry_id Number Address
1 1 Chand Pura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

5 rows in set (0.00 sec)

Regular Join:

If apply a regular JOIN (with out use of these keywords INNER, OUTER, LEFT or RIGHT), then I get all records that match in the appropriate way in the two tables, and records in both incoming tables that do not match are not reported:

mysql> select name, phone, address

from person join information

on person.number = information.number;

OR

mysql> select name, phone, address

from person,information where

person.number = information.number;

Output:

name Phone Address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP (INDIA)

4 rows in set (0.01 sec)

LEFT JOIN:
If I apply left joint with using keyword LEFT JOIN

mysql> select name, phone, address
from person left join information on person.number = information.number;

Output:

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Urooj 0120 234567 NULL
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP

5 rows in set (0.00 sec)

RIGHT JOIN:
If I apply right joint with using keyword RIGHT JOIN

mysql> select name, phone, address
from person right join information on person.number = information.number;

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP
NULL NULL Faizadab UP (INDIA)

5 rows in set (0.00 sec)

OUTER JOIN: Outer join does not support RDBMS.

Article written by admin

Delete Duplicate rows from the table?

January 10, 2010 | Filed Under MySQL | Leave a Comment

A- Suppose that Student is the table

CREATE TABLE `student` (
`stid` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`phone` varchar(15) NOT NULL,
PRIMARY KEY (`stid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;


– Dumping data for table `student`

INSERT INTO `student` VALUES (1, ‘abc’, ‘9891215668′);
INSERT INTO `student` VALUES (2, ‘abc’, ‘9891215668′);
INSERT INTO `student` VALUES (3, ‘abc’, ‘9891215668′);

Student :
+————+————–+——+
stid | name | phone
+————+————–+——+
1 | abc | 9891215668
2 | abc |9891215668
3 | abc |9891215668
+————+————–+——+

Need to delete all duplicate rows.

MYSQl->
DELETE duplicate_rows. * FROM student AS duplicate_rows INNER JOIN (
SELECT name, MIN( stid ) AS st_id
FROM student
GROUP BY name
HAVING count( * ) >1
) AS exp_rows ON exp_rows.name = duplicate_rows.name
AND exp_rows.st_id <> duplicate _rows.stid;

Output:
+————+————–+——+
stid | name | phone
+————+————–+——+
1 | abc | 9891215668
+————+————–+——+

Article written by admin

How to find the second highest salary from emp table?

December 28, 2009 | Filed Under MySQL | Leave a Comment

There are number of examples to get second highest salary from EMP table.

example-select sal from(select sal from
(select distinct sal from emp order by sal desc)
where rownum<=2 order by sal asc)
where rownum=1;

example- SELECT MAX(SAL) FROM EMP WHERE SAL NOT IN (SELECT MAX(SAL) FROM EMP)

where EMP is table name,SAL is salary colum.

Article written by admin

Difference between insert,update and modify?

December 24, 2009 | Filed Under MySQL | Leave a Comment

INSERT – Add a new record into the database table.

UPDATE – If record is available its update the record otherwise it creates a new record.

MODIFY – If record is available it modifies otherwise it wont modify.

Article written by urooj

How to display nth highest record in a table for example How to display 4th highest (salary) record from customer table.

December 24, 2009 | Filed Under MySQL | Leave a Comment

Query: SELECT sal FROM `emp` order by sal desc limit (n-1),

1If the question: “how to display 4th highest (salary) record from customer table.”The query will SELECT sal FROM `emp`

order by sal desc limit 3,1

Article written by urooj

What is the difference between inner join and outer join?

December 24, 2009 | Filed Under MySQL | Leave a Comment

Inner join displays rows from table where the data is available in both the tables, where in outer join we can configure it to bring out rows from one table where the data is missing in other table for the corresponding rows.

Say you have one table of CUSTOMERS and one table of ORDERS. Each row in the ORDERS table has a reference (foreign key reference) to a customer id which represents what customer placed that order. If you want to run a query that lists the orders along with the names of the customers who ordered them (since a customer id number itself it pretty useless), you will want to execute a join query:

SELECT CUSTOMERS.NAME, ORDERS.NAME
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

If for some reason, you wanted the query results to return all customer name regardless of whether they placed an order, you can use one of two types of OUTER JOINS, in this case, a LEFT JOIN:

SELECT CUSTOMERS.NAME, ORDERS.NAME
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

Article written by urooj

© PHPInterviewQuestion.com 2009 - 2010

eXTReMe Tracker