sql query list

SQL Statement Syntax
AND / OR SELECT Column_Name
FROM Table_Name
WHERE condition
AND|OR condition
Example : SELECT * FROM mytable WHERE name1=’name1′ AND name2=’name2′ OR name3=’name3′
ALTER TABLE ALTER TABLE Table_Name
ADD Column_Name datatype
or
ALTER TABLE Table_Name
DROP COLUMN Column_Name
Example : ALTER TABLE `table1` ADD `name2` VARCHAR( 10 ) NOT NULL AFTER `name` ;
ALTER TABLE table1 DROP name2
AS (alias) SELECT Column_Name AS column_alias
FROM Table_Name
or
SELECT Column_Name
FROM Table_Name  AS table_alias
Example : SELECT t1.name, table2.name FROM `table1` AS t1 inner join table2 on t1.name=table2.name
BETWEEN SELECT Column_Name
FROM Table_Name
WHERE Column_Name
BETWEEN value1 AND value2
CREATE DATABASE CREATE DATABASE database_name
CREATE TABLE CREATE TABLE Table_Name
(
Column_Name1 data_type,
Column_Name2 data_type,
Column_Name2 data_type,

)
Example : CREATE TABLE `database`.`table22` (`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,`address` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` ))
CREATE INDEX CREATE INDEX index_name
ON Table_Name (Column_Name)
or
CREATE UNIQUE INDEX index_name
ON Table_Name (Column_Name)
CREATE VIEW CREATE VIEW view_name AS
SELECT Column_Name
FROM Table_Name
WHERE condition
DELETE DELETE FROM Table_Name
WHERE some_column=some_value
or
DELETE FROM Table_Name
(Note: Deletes the entire table!!)
DELETE * FROM Table_Name
(Note: Deletes the entire table!!)
DROP DATABASE DROP DATABASE database_name
DROP INDEX DROP INDEX Table_Name.index_name (SQL Server)
DROP INDEX index_name ON Table_Name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE Table_Name
DROP INDEX index_name (MySQL)
DROP TABLE DROP TABLE Table_Name
GROUP BY SELECT Column_Name, aggregate_function(Column_Name)
FROM Table_Name
WHERE Column_Name operator value
GROUP BY Column_Name
HAVING SELECT Column_Name, aggregate_function(Column_Name)
FROM Table_Name
WHERE Column_Name operator value
GROUP BY Column_Name
HAVING aggregate_function(Column_Name) operator value
IN SELECT Column_Name
FROM Table_Name
WHERE Column_Name
IN (value1,value2,..)
INSERT INTO INSERT INTO Table_Name
VALUES (value1, value2, value3,….)
or
INSERT INTO Table_Name
(column1, column2, column3,…)
VALUES (value1, value2, value3,….)
INNER JOIN SELECT Column_Name
FROM Table_Name1
INNER JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
LEFT JOIN SELECT Column_Name
FROM Table_Name1
LEFT JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
RIGHT JOIN SELECT Column_Name
FROM Table_Name1
RIGHT JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
FULL JOIN SELECT Column_Name
FROM Table_Name1
FULL JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
LIKE SELECT Column_Name
FROM Table_Name
WHERE Column_Name LIKE pattern
ORDER BY SELECT Column_Name
FROM Table_Name
ORDER BY Column_Name [ASC|DESC]
SELECT SELECT Column_Name
FROM Table_Name
SELECT * SELECT *
FROM Table_Name
SELECT DISTINCT SELECT DISTINCT Column_Name
FROM Table_Name
SELECT INTO SELECT *
INTO new_Table_Name [IN externaldatabase]
FROM old_Table_Name
or
SELECT Column_Name
INTO new_Table_Name [IN externaldatabase]
FROM old_Table_Name
SELECT TOP SELECT TOP number|percent Column_Name
FROM Table_Name
TRUNCATE TABLE TRUNCATE TABLE Table_Name
UNION SELECT Column_Name FROM Table_Name1
UNION
SELECT Column_Name FROM Table_Name2
UNION ALL SELECT Column_Name FROM Table_Name1
UNION ALL
SELECT Column_Name FROM Table_Name2
UPDATE UPDATE Table_Name
SET column1=value, column2=value,…
WHERE some_column=some_value
WHERE SELECT Column_Name
FROM Table_Name
WHERE Column_Name operator value

how to copy data from one table to another table in mysql

INSERT  INTO  TABLE2  SELECT  *  FROM  TABLE1





INSERT INTO TABLE2 (COL1, COL2, COL3) SELECT COL1, COL4, COL7 FROM TABLE1

copy table one database to another database 

CREATE TABLE database2.`tablename2` SELECT * FROM database1.`tablename1`

NOTE:
it must you have database1 , database2 and tablename1
Create similar table in mysql 

CREATE TABLE new_table_name LIKE tablename2;



SELECT Query in mysql

table1

id name mobile no dob
       
     



1.   ” SELECT *  FROM table1 ”  ;


2. ” SELECT *  FROM table1 WHERE  id= ‘1’   ORDER BY  id  DESC ”  ;


3. ” SELECT *  FROM table1 WHERE  dob BETWEEN  date1 AND  date2  ”  ;

4. ” SELECT *  FROM table1 WHERE id=’1′  AND  dob BETWEEN  date1 AND  date2  ”  ;

5.  ” SELECT *  FROM table1 WHERE  id= ‘1’  LIMIT  10″  ;

6.  ” SELECT *  FROM table1 WHERE  id= ‘1’  LIMIT   0  , 10 ”  ;

7.   ” SELECT *  FROM table1 WHERE  name LIKE  ‘%name%’ ”  ;

8.  ” SELECT *  FROM table1 WHERE  name LIKE  ‘%name%’  ORDER BY  id  ASC  LIMIT 10 ”  ;

9.   group by

10  . having 

11  or

12  not

13  as 

14