Home >>MySQL Tutorial >MySQL Common Queries
SQL Statement | What It Does |
---|---|
CREATE DATABASE database-name | Creates a new database |
DROP DATABASE database-name | Deletes a database |
CREATE TABLE table-name(field1, field2, . . .) | Creates a new table |
DROP TABLE table-name | Deletes a table |
RENAME Table Old-table-name TO New-table-name | Renames a table |
ALTER TABLE table-name ADD(field1, field2, . . .) | Add fields in existing table |
ALTER TABLE table-name DROP(field1) | Deletes fields from table |
INSERT INTO table-name (field1, field2, . . . ) VALUES(value1, value2, . . .) | Inserts a new record into a table with specified values |
UPDATE table-name SET field1=value1, field2=value2,... [WHERE condition] | Updates records in a table with new values |
DELETE FROM table-name [WHERE condition] | Deletes records from a table |
SELECT field1, field2, . . . FROM table-name [WHERE condition] | Retrieves matching records from a table |
SELECT * FROM table-name | Retrieves all records from a table |
Alter table reg drop id; // here table name is "reg" and auto_increment filed is "id" Alter table reg auto_increment=1; //assign auto_increment=1(if you want to start from 100 then auto_increment=100) Alter table reg ADD id bigint unsigned not null auto_increment primary key first; //Now add a new filed "id", (define auto_increment)
// here "employee" is table name select count(*) from employee
// here "employee" is table name select max(salary) from employee;
select max(salary) from employee where salary < ( select max(salary) from employee);
// here "employee" is table name select min(salary) from employee;
select min(salary) from employee where salary > ( select min(salary) from employee); // here "employee" is table name
// here "employee" is table name select * from employee where salary = (select max(salary) from employee)
// here "employee" is table name select * from employee ORDER BY salary desc;
select * from employee ORDER BY salary asc; //OR select * from employee; // here "employee" is table name
select * from employee limit 5,10; // OR Select * from employee limit 10 OFFSET 5; // both queries will give the same result.
select * from employee ORDER BY id Desc limit 1 ; // here "employee" is table name and it returns the last entry
select * from employee ORDER BY id ASC limit 1 ; // here "employee" is table name and it returns the first entry
(select id,name,salary from employee ORDER BY id Asc limit 1)
UNION
(select id, name, salary from employee ORDER BY id Desc limit 1);
// here "employee" is table name and it returns the id,name, and salary of first user and last user
select * from student where age between 25 and 40; // here "student" is table name
select * from employee where date between “2012-04-01” and “2012-04-10”; // here "employee" is table name. //Year,month and date separated by '-' so pass date as a string “2012-04-01”.
SELECT DISTINCT name FROM student; // here "student" is table name while "name" is field name
select * from employee where daily_page=100 OR daily_page=150 OR daily_page=200; // here "employee" is table name.it return the details of employee whose daily_page=100 or 150 or 200
Select * from employee where daily_page IN(100,150,200); // here "employee" is table name.it return the details of employee whose daily_page=100 or 150 or 200
CREATE TABLE Person ( P_id int auto_increment primary key, name char(30) not null, email varchar(50) unique key )
CREATE TABLE Orders ( O_Id int auto_increment PRIMARY KEY , OrderNo int NOT NULL, P_id int, FOREIGN KEY (P_id) REFERENCES Person(P_id) ); // Here P_id field is foreign key(the primary key of Person table)
SELECT * FROM stu_info WHERE gender LIKE 'm%';Example 2: Example to select the stuinfo whose username starts from n.
SELECT * FROM stuinfo WHERE userName LIKE 'n%’Example 3: Example to select the stdinfo whose username contains n .
SELECT * FROM stuinfo WHERE userName LIKE '%n%';