use DB1;

create table Employee1 (
EMPLOYEE_ID int not null primary key,
FIRST_NAME varchar(255) not null,
LAST_NAME varchar(255) not null,
SALARY int not null,
JOINING_DATE varchar(255) not null,
DEPARTMENT varchar(255) not null,
MANAGER_ID int
);

drop table Employee1;

INSERT INTO `DB1`.`Employee1`
(`EMPLOYEE_ID`,
`FIRST_NAME`,
`LAST_NAME`,
`SALARY`,
`JOINING_DATE`,
`DEPARTMENT`,
`MANAGER_ID`)
VALUES(1, "John","Abraham", 1000000, "01-JAN-13 12.00.00 AM", "Banking",Null),
(2, "Michael", "Clarke", 800000," 01-JAN-13 12.00.00 AM","Insurance",1),
( 3, "Roy"," Thomas", 700000," 01-FEB-13 12.00.00 AM","Banking",1 ),
( 4 ,"Tom"," Jose", 600000 ,"01-FEB-13 12.00.00 AM","Insurance",2),
(5 ,"Jerry"," Pinto", 650000, "01-FEB-13 12.00.00 AM", "Insurance",3),
( 6 ,"Philip"," Mathew", 750000, "01-JAN-13 12.00.00 AM","Services",3),
(7 ,"TestName1"," 123 ",650000," 01-JAN-13 12.00.00 AM","Services",2 ),
(8 ,"TestName2"," Lname%", 600000, "01-FEB-13 12.00.00 AM","Insurance",2 );

-- 1. Create a view Select Banking as 'Bank Dept', Insurance as 'Insurance Dept' and Services as 'Services Dept' from employee table 
	create view Bank_Dept as select * from Employee1 where DEPARTMENT = 'Banking';
    create view Insurance_Dept as select * from Employee1 where DEPARTMENT = 'Insurance';
    create view Services_Dept as select * from Employee1 where DEPARTMENT = 'Services';
    
    select * from Bank_Dept;
    select * from Insurance_Dept;
    select * from Services_Dept;
    
    

-- 2. Select employee details from employee table if data exists in incentive table ? 
	 create view emp_details as select e.EMPLOYEE_ID, e.FIRST_NAME, e.LAST_NAME, e.SALARY, e.JOINING_DATE, e.DEPARTMENT, e.MANAGER_ID from 
     Employee1 as e join Incentives as i on e.EMPLOYEE_ID =i.EMPLOYEE_REF_ID;
     
	 select * from emp_details;
-- 3. Find Salary of the employee whose salary is more than Roy Salary 
	select  * from Employee1 where SALARY > (select SALARY from Employee1 where FIRST_NAME = 'Roy');

-- 4. Create a view to select FirstName,LastName,Salary,JoiningDate,IncentiveDate and IncentiveAmount
	create view view1 as select e.FIRST_NAME, e.LAST_NAME, e.SALARY, e.JOINING_DATE, i.INCENTIVE_DATE,i.INCENTIVE_AMOUNT  from 
     Employee1 as e join Incentives as i on e.EMPLOYEE_ID =i.EMPLOYEE_REF_ID;
     
     select * from view1;

-- 5. Create a view to select Select first_name, incentive amount from employee and incentives table for those employees who have incentives
--  and incentive amount greater than 3000 
	create view view2 as select e.FIRST_NAME,i.INCENTIVE_AMOUNT  from 
     Employee1 as e join Incentives as i on e.EMPLOYEE_ID =i.EMPLOYEE_REF_ID where i.INCENTIVE_AMOUNT > 3000;
     
     select *from view2;
	use db2_tables;
-- 6. Create a View to Find the names (first_name, last_name), job, department number, and department name of 
-- the employees who work in London.

		create view emp_details as select e.FirstName, e.LastName, e.JobId,d.DepartmentID,d.DepartmentName,l.City
		 from Employees e join Departments d on e.DepartmentID = d.DepartmentID join
        Locations as l on d.LocationID = l.LocationID where l.City = 'London';
	
		select * from emp_details;
-- 7. Create a View to get the department name and number of employees in the department. 
	create view emp_dept as select d.DepartmentName,count(e.EmployeeID) as Total_Employee  from 
     Employees as e join Departments as d on e.DepartmentID =d.DepartmentID group by e.DepartmentID;
     
     select * from emp_dept;
	
-- 8. Find the employee ID, job title, number of days between ending date and starting date for all jobs in department 90 from job history. 
	
		select EmployeeID, JobID as Job_Title, datediff(EndDate,StartDate) as Number_Of_Days from JobHistory where DepartmentID=90;

-- 9. Write a query to display the department name, manager name, and city. 

		select d.DepartmentName, e.FirstName as ManagerName, l.City from Departments d join Employees e on d.ManagerID = e.EmployeeID join Locations as l on d.LocationID = l.LocationID;

-- 10. Create a View to display department name, name (first_name, last_name), hire date, salary of the manager 
-- for all managers whose experience is more than 15 years.

	create view experience_details as select d.DepartmentName, concat(e.FirstName,' ',e.LastName) as FullName, e.HireDate, e.Salary from Departments as d join Employees as e on
    d.ManagerID = e.EmployeeID where (year(curdate()) - year(e.HireDate)) > 15;
    
    select * from experience_details;
    
    select year(curdate()) - year(HireDate) from Employees;

 
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;