create table Employees(EMPLOYEE_ID INT,FIRST_NAME varchar(20),LAST_NAME varchar(20),EMAIL varchar(50),PHONE_NUMBER long,HIRE_DATE date,JOB_ID int,SALARY long,COMMISSION_PCT long ,MANAGER_ID int,DEPARTMENT_ID int);
insert into Employees(EMPLOYEE_ID ,FIRST_NAME ,LAST_NAME ,EMAIL ,PHONE_NUMBER ,HIRE_DATE ,JOB_ID ,SALARY ,COMMISSION_PCT ,MANAGER_ID ,DEPARTMENT_ID )
values
(1,"vansh","tuteja","123@gmail","1234567890","2019-03-05",11,120000,20000,111,1111),
(2,"rohit","tuteja","13@gmail","1434567895","2021-03-01",22,100000,15000,222,2222),
(3,"ansh","sharma","234@gmail","1634567892","2020-04-01",33,90000,2000,333,3333),
(4,"ram","arora","345@gmail","1234527891","2016-02-02",44,150000,25000,444,4444);
select * from Employees;
drop table Employees;

create table job_history(EMPLOYEE_ID int,START_DATE date,END_DATE date,JOB_ID int,DEPARTMENT_ID int);
insert into job_history(EMPLOYEE_ID,START_DATE,END_DATE,JOB_ID,DEPARTMENT_ID)
values
(1,"2019-03-12","2024-03-05",11,1111),
(2,"2021-03-16","2024-03-01",22,2222),
(3,"2020-04-23","2023-04-01",33,3333),
(4,"2016-02-20","2018-02-02",44,4444);
select * from job_history;
drop table job_history;
create table locations(LOCATION_ID int,STREET_ADDRESS varchar(50),POSTAL_CODE long,CITY varchar(20),STATE_PROVINCE varchar(20),COUNTRY_ID int);
insert into locations(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID)
values
(21,"ploatno.16,shivam nagar",302017,"jaipur","rajasthan",211),
(22,"1 ka 1 shivaji park",301001,"alwar","rajasthan",222),
(23,"street 12",302001,"delhi","delhi",233),
(24,"plotno.18 shiv nagar",301002,"kota","rajasthan",244);
select * from locations;

create table jobs(JOB_ID int ,JOB_TITLE varchar(20),MIN_SALARY long,MAX_SALARY long);
insert into jobs(JOB_ID,JOB_TITLE,MIN_SALARY,MAX_SALARY)
values
(11,"manager",50000,200000),
(22,"team leader",25000,100000),
(33,"employee",20000,90000),
(44,"worker",15000,40000);
select * from jobs;
drop table jobs;

create table counteries(COUNTRY_ID int,COUNTRY_NAME varchar(20),REGION_ID varchar(20));
insert into counteries(COUNTRY_ID,COUNTRY_NAME,REGION_ID)
values
(211,"INDIA",311),
(222,"bharat",322),
(233,"INDIA",333),
(244,"bharat",344);
select * from counteries;

create table departments(DEPARTMENT_ID int,DEPARTMENT_NAME varchar(20),MANAGER_ID int,LOCATION_ID int);
insert into departments(DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID,LOCATION_ID)
values

(1111,"Data analysis",111,21),
(2222,"Data management",222,22),
(3333,"Programming",333,23),
(4444,"Testing",444,24),
(5555,"Data analysis",555,25);
select * from departments;

create table regions(REGION_ID int,REGION_NAME varchar(20));
insert into regions(REGION_ID,REGION_NAME)
values
(311,"Asia"),
(322,"Asia"),
(333,"Asia"),
(344,"Asia");
select * from regions;

  
select departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME,departments.LOCATION_ID,locations.STREET_ADDRESS,locations.CITY,locations.STATE_PROVINCE,counteries.COUNTRY_NAME
from ((locations
inner join departments on locations.LOCATION_ID = departments.LOCATION_ID)
inner join counteries on locations.COUNTRY_ID = counteries.COUNTRY_ID);

select Employees.FIRST_NAME,Employees.LAST_NAME,Employees.DEPARTMENT_ID, departments.DEPARTMENT_NAME
from Employees
inner join departments on Employees.MANAGER_ID = departments.MANAGER_ID;

select Employees.FIRST_NAME,Employees.LAST_NAME,jobs.JOB_TITLE,Employees.DEPARTMENT_ID, departments.DEPARTMENT_NAME
from ((Employees
inner join jobs on Employees.JOB_ID = jobs.JOB_ID)
inner join departments on Employees.MANAGER_ID = departments.MANAGER_ID);

select EMPLOYEE_ID,LAST_NAME,MANAGER_ID from Employees;

select Employees.FIRST_NAME,Employees.LAST_NAME,Employees.HIRE_DATE 
from Employees
where HIRE_DATE > "2019-03-05";

select DEPARTMENT_NAME,count(DEPARTMENT_NAME) from departments group by DEPARTMENT_NAME;

select  job_history.EMPLOYEE_ID,jobs.JOB_TITLE,DATEDIFF(job_history.END_DATE,job_history.START_DATE) AS DURATION_IN_DAYS
from job_history
inner join jobs on jobs.JOB_ID = job_history.JOB_ID
where job_history.JOB_ID = 22;

select departments.DEPARTMENT_ID,departments.DEPARTMENT_NAME,Employees.FIRST_NAME
from departments
inner join Employees on departments.DEPARTMENT_ID = Employees.DEPARTMENT_ID;

select departments.DEPARTMENT_NAME,concat(Employees.FIRST_NAME," ",Employees.LAST_NAME)AS Manager_Name,locations.CITY 
from ((departments
inner join locations on locations.LOCATION_ID = departments.LOCATION_ID)
inner join Employees on Employees.DEPARTMENT_ID = departments.DEPARTMENT_ID);

select JOB_TITLE,((MIN_SALARY+MAX_SALARY)/2) as AVG_SALARY
from jobs;

select jobs.JOB_TITLE,concat(Employees.FIRST_NAME," ",Employees.LAST_NAME)AS EMPLOYEE_NAME,
(Employees.SALARY - jobs.MIN_SALARY)AS SALARY_Difference
from Employees
join jobs on Employees.JOB_ID = jobs.JOB_ID;

select job_history.*
from job_history
join Employees on Employees.EMPLOYEE_ID = job_history.EMPLOYEE_ID
where Employees.SALARY >100000 ;

select 
departments.DEPARTMENT_NAME,concat(Employees.FIRST_NAME," ",Employees.LAST_NAME)as MANAGER_NAME,
Employees.HIRE_DATE AS MANAGER_HIRE_DATE,
Employees.SALARY AS MANGER_SALARY
FROM
DEPARTMENTS
join
Employees ON departments.MANAGER_ID=Employees.MANAGER_ID
WHERE 
datediff(NOW(),Employees.HIRE_DATE) > 4*365;


 
by

MySQL online editor

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.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

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 } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;