CREATE TABLE dept( deptno int(2) not null primary key, dname varchar(50) not null, location varchar(50) not null); CREATE TABLE emp( empno int(4) not null primary key, ename varchar(50) not null, job varchar(50) not null, mgr int(4), hiredate date, sal decimal(10,2), comm decimal(10,2), deptno int(2) not null); insert into dept values (10,'Accounting','New York'); insert into dept values (20,'Research','Dallas'); insert into dept values (30,'Sales','Chicago'); insert into dept values (40,'Operations','Boston'); insert into emp values (7369,'SMITH','CLERK',7902,'93/6/13',800,0.00,20); insert into emp values (7499,'ALLEN','SALESMAN',7698,'98/8/15',1600,300,30); insert into emp values (7521,'WARD','SALESMAN',7698,'96/3/26',1250,500,30); insert into emp values (7566,'JONES','MANAGER',7839,'95/10/31',2975,null,20); insert into emp values (7698,'BLAKE','MANAGER',7839,'92/6/11',2850,null,30); insert into emp values (7782,'CLARK','MANAGER',7839,'93/5/14',2450,null,10); insert into emp values (7788,'SCOTT','ANALYST',7566,'96/3/5',3000,null,20); insert into emp values (7839,'KING','PRESIDENT',null,'90/6/9',5000,0,10); insert into emp values (7844,'TURNER','SALESMAN',7698,'95/6/4',1500,0,30); insert into emp values (7876,'ADAMS','CLERK',7788,'99/6/4',1100,null,20); insert into emp values (7900,'JAMES','CLERK',7698,'00/6/23',950,null,30); insert into emp values (7934,'MILLER','CLERK',7782,'00/1/21',1300,null,10); insert into emp values (7902,'FORD','ANALYST',7566,'97/12/5',3000,null,20); insert into emp values (7654,'MARTIN','SALESMAN',7698,'98/12/5',1250,1400,30); select sqrt(25); select pow(2,3); select mod(11,3); select round(3/11,2); select floor(45.789), ceil(45.789); select DATE_FORMAT(NOW(), '%D %M %Y') as currdate; select TIMESTAMPDIFF(MONTH, '1998-01-28',now()); select empno, ename, TIMESTAMPDIFF(YEAR, hiredate, now()) as exp from emp; select empno, ename, hiredate from emp where year(hiredate)<1996; select empno, ename, hiredate from emp where monthname(hiredate)='June'; select upper(ename), lower(ename), concat(upper(substr(ename,1,1)), lower(substr(ename,2))) as sentence from emp; select substr('abcedfgh',1, 5); select count(*) from emp; select count(*) from dept; select count(empno) from emp where deptno=10; select count(empno) from emp where sal>3000; select sum(sal), avg(sal), min(sal), max(sal) from emp; select empno, sal+ifnull(comm,0) from emp; select ename, sal, "on target" as msg from emp where sal=1500 union select ename, sal, "less" as msg from emp where sal<1500 union select ename, sal, "more" as msg from emp where sal>1500; select empno, ename from emp where ename like "%a%"; select empno, ename, sal from emp where sal>(select sal from emp where ename like "smith"); select count(empno) from emp as e1 where sal>(select sal from emp as e2 where e1.mgr=e2.empno); select empno, ename, sal from emp as e1 where sal>(select avg(sal) from emp as e2); select empno, ename, job from emp as e1 where e1.job like "salesman" and e1.sal = (select max(sal) from emp where job like "salesman"); select empno, ename from emp as e1 where e1.hiredate<(select e2.hiredate from emp as e2 where e1.mgr=e2.empno); select empno, ename, job, sal from emp where sal in (select min(sal) from emp group by job) order by sal desc;
Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.
MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
ALTER TABLE Table_name ADD column_name datatype;
INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');
TRUNCATE table table_name;
DROP TABLE table_name;
RENAME TABLE table_name1 to new_table_name1;
--Line1;
/* Line1,
Line2 */
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Note: Column names are optional.
INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');
SELECT column1, column2, ...
FROM table_name
[where condition];
SELECT * FROM EMPLOYEE where dept ='sales';
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001';
DELETE FROM table_name where condition;
DELETE from EMPLOYEE where empId='0001';
CREATE INDEX index_name on table_name(column_name);
CREATE UNIQUE INDEX index_name on table_name(column_name);
DROP INDEX index_name ON table_name;
Creating a View:
CREATE VIEW View_name AS
Query;
SELECT * FROM View_name;
ALTER View View_name AS
Query;
DROP VIEW View_name;
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */
DROP TRIGGER [IF EXISTS] trigger_name;
CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;
CALL sp_name;
DROP PROCEDURE sp_name;
SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;
SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;
SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;
SELECT select_list from TABLE1 CROSS JOIN TABLE2;