-- create table 
CREATE TABLE employees (
  EMPNO INTEGER PRIMARY KEY,
  ENAME varchar(255) NOT NULL,
  JOB varchar(255) NOT NULL,
  MGR INTEGER,
  HIREDATE date NOT NULL,
  Salary INTEGER NOT NULL,
  COMM varchar(255),
  DEPTNO INTEGER NOT NULL
);
-- insert 
INSERT INTO employees 
VALUES (7369, 'SMITH', 'CLERK', 7902, '1980/12/17', 800, NULL, 20),
       (7499, 'ALLEN', 'SALESMAN', 7698, '1981/02/20', 1600, 300, 30),
       (7521, 'WARD', 'SALESMAN', 7698, '1981/02/22', 1250, 500, 30),
       (7566, 'JONES', 'MANAGER', 7839, '1980/08/02', 2975, NULL, 20),
       (7654, 'MARTIN', 'SALESMAN', 7698, '1981/09/28', 1250, 1400, 30),
       (7698, 'BLAKE', 'MANAGER', 7839, '1981/05/01', 2850, NULL, 30),
       (7782, 'CLARK', 'MANAGER', 7839, '1981/06/09', 2450, NULL, 20),
       (7788, 'SCOTT', 'ANALYST', 7566, '1982/12/09', 3100, NULL, 30),
       (7839, 'KING', 'PRESIDENT',NULL , '1981/11/17', 5000, NULL, 10),
       (7844, 'TURNER', 'SALESMAN', 7698, '1981/09/08', 1500, 0, 30),
       (7876, 'ADAMS', 'CLERK', 7788, '1983/01/12', 1100, NULL, 20),
       (7900, 'JAMES', 'CLERK', 7698, '1981/12/03', 950, NULL, 30),
       (7902, 'FORD', 'ANALYST', 7566, '1981/12/03', 3000, NULL, 20),
       (7934, 'MILLER', 'CLERK', 7782, '1983/01/23', 1300, NULL, 20);
       
-- CREATE TABLE 2" 

CREATE TABLE depts (
  DEPTNO INTEGER PRIMARY KEY,
  DNAME varchar(255) NOT NULL,
  Location varchar(255) NOT NULL
);
-- insert 
INSERT INTO depts 
VALUES (10, 'accounting', 'New York'),
      (20, 'research', 'Dallas'),
      (30, 'sales', 'Chicago'),
      (40, 'operations', 'Boston');

-- fetch 

 
SELECT * FROM employees











-- Q1(A): Display all the information of the EMP table?
-- select * from employee
-- select * from dept

-- Q1 (B):  Display unique Jobs from EMP table?
/* SELECT DISTINCT JOB AS UNIQUE_JOBS
FROM employee*/ 

-- Q1(C): List the emps in the asc order of their Salaries
/* SELECT *
FROM employee
ORDER BY Salary */ 
-- Q1(D); List the details of the emps in asc order of the Dptnos and desc of Jobs?
/*SELECT * 
FROM employee
ORDER BY DEPTNO, JOB DESC*/
-- Q1(E): Display all the unique job groups in the descending order?
/* SELECT DISTINCT JOB 
FROM employee
ORDER BY JOB DESC*/ 

-- Q1(F): Display all the details of all ‘Mgrs’
/*SELECT * 
FROM employee
WHERE JOB = ('MANAGER')*/ 

-- Q1(G): List the emps who joined before 1981.
/*SELECT *
FROM employee
WHERE HIREDATE < '1981/01/01'*/ 

-- Q1(H) List the Empno, Ename, Sal, Daily sal of all emps in the asc order of Annsal.     -- check1 
/* SELECT EMPNO, ENAME, Salary,  Salary/30 AS DAILY_SALARY, 12*Salary AS ANNUAL_SALARY 
FROM employee
ORDER BY ANNUAL_SALARY*/

-- 2.9. Display the Empno, Ename, job, Hiredate, Exp of all Mgrs                       -- check2
/*SELECT empno, Ename, job, Hiredate, TIMESTAMPDIFF(year, Hiredate, CURRENT_DATE) as YOE
FROM employee
WHERE JOB = ('MANAGER')*/ 

-- 2.10. List the Empno, Ename, Sal, Exp of all emps working for Mgr 7698.
/*SELECT Empno, Ename, Salary, TIMESTAMPDIFF(year, Hiredate, CURRENT_DATE) as YOE
FROM employee
WHERE MGR = (7698)*/ 

-- 2.11. Display all the details of the emps whose Comm. Is more than their Sal.
/*SELECT * 
FROM employee
WHERE COMM > Salary*/ 

-- 2.12: List the emps in the asc order of Designations of those joined after the second half of 1981

/*SELECT *
FROM employee
WHERE HIREDATE   > '1981/06/30' 
ORDER BY JOB*/ 
-- List the emps along with their Exp and Daily Sal is more than Rs.100. 
/*SELECT * 
FROM employee
WHERE Salary/30 > 100*/ 

-- 2.14. List the emps who are either ‘CLERK’ or ‘ANALYST’ in the Desc order

/*SELECT * 
FROM employee
WHERE JOB IN ('CLERK', 'ANALYST')
ORDER BY JOB DESC*/ 

-- 2.15. List the emps who joined on 1-MAY-81,3-DEC-81,17-DEC-81,19-JAN80 in asc order of seniority.
/*SELECT * 
FROM employee
WHERE HIREDATE IN ('1981/05/01', '1981/12/03', '1981/12/17', '1980/01/19') 
ORDER BY HIREDATE*/ 

-- 2.16. List the emp who are working for the Deptno 10 or20
/*SELECT *
FROM employee
WHERE DEPTNO IN (10,20)*/ 

-- 2.17. List the emps who are joined in the year 81.                                    -- CHECK3 
/*SELECT *
FROM employee
WHERE  EXTRACT(YEAR FROM HIREDATE) = 1981*/  
-- 2.18. List the emps who are joined in the month of Aug 1980.
/*SELECT * 
FROM employee
WHERE EXTRACT(YEAR FROM HIREDATE) = 1980 AND EXTRACT(MONTH FROM HIREDATE) = 08*/ 

/* SELECT * 
FROM employee
WHERE HIREDATE  BETWEEN ('1980/08/01') AND ('1980/08/31')*/ 

-- 2.19. List the emps Who Annual sal ranging from 22000 and 45000.
/* SELECT * 
FROM employee
WHERE Salary*12 BETWEEN 22000 AND 45000*/ 

-- 2.20. List the Enames those are having five characters in their Names.
/*SELECT * 
FROM employee
WHERE LENGTH(ENAME) = 5*/ 

-- 2.21. List the Enames those are starting with ‘S’ and with five characters.
/*SELECT * 
FROM employee
WHERE ENAME LIKE ('S%') AND LENGTH(ENAME) = 5*/ 

-- 2.22. List the emps those are having four chars and third character must be ‘r’.
/*SELECT * 
FROM employee
WHERE ENAME LIKE ('__R_')*/ 

-- 2.23. List the Five character names starting with ‘S’ and ending with ‘H’.

/*SELECT * 
FROM employee
WHERE ENAME LIKE ('S%H') AND LENGTH(ENAME) = 5*/ 

-- 2.24. List the emps who joined in January
/*SELECT * 
FROM employee
WHERE EXTRACT(MONTH FROM HIREDATE) = 01*/ 
 
-- 2.25. List the emps who joined in the month of which second character is ‘a’.                          --CHECK 4 
/* WITH CTE AS 
(
SELECT *,
    CASE 
        WHEN EXTRACT(MONTH FROM HIREDATE) = 1 THEN 'JAN'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 2 THEN 'FEB'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 3 THEN 'MAR'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 4 THEN 'APR'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 5 THEN 'MAY'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 6 THEN 'JUN'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 7 THEN 'JUL'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 8 THEN 'AUG'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 9 THEN 'SEP'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 10 THEN 'OCT'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 11 THEN 'NOV'
        WHEN EXTRACT(MONTH FROM HIREDATE) = 12 THEN 'DEC'
    END AS MONTH_JOINED
FROM employee
) 
SELECT * 
FROM CTE 
WHERE MONTH_JOINED LIKE ('_A_')*/ 

-- ANOTHER WAY OF WRITING CASE STATEMENT 
/* SELECT *,
    CASE EXTRACT(MONTH FROM HIREDATE)
        WHEN 1 THEN 'JAN'
        WHEN 2 THEN 'FEB'
        WHEN 3 THEN 'MAR'
        WHEN 4 THEN 'APR'
        WHEN 5 THEN 'MAY'
        WHEN 6 THEN 'JUN'
        WHEN 7 THEN 'JUL'
        WHEN 8 THEN 'AUG'
        WHEN 9 THEN 'SEP'
        WHEN 10 THEN 'OCT'
        WHEN 11 THEN 'NOV'
        WHEN 12 THEN 'DEC'
    END AS MONTH_JOINED
FROM employee*/ 

-- 2.26. List the emps whose Sal is four digit number ending with Zero.
/* SELECT * 
FROM employee
WHERE SALARY LIKE ('___0')*/ 

-- 2.27. List the emps whose names having a character set ‘ll’ together.    -- %: for matching zero or more characters. 
/* SELECT * 
FROM employee
WHERE ENAME  LIKE ('%LL%')*/ 

-- 2.28. List the emps those who joined in 80’s.
/* SELECT * 
FROM employee
WHERE EXTRACT(YEAR FROM HIREDATE) = 1980*/ 

-- 2.29. List the emps who does not belong to Deptno 20
/*SELECT *
FROM employee
WHERE DEPTNO NOT IN  (20)*/ 

-- 2.30. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.
/*SELECT * 
FROM employee
WHERE JOB NOT IN ('PRESIDENT', 'MANAGER')
ORDER BY Salary*/ 

-- 2.31. List all the emps who joined before or after 1981.
/*SELECT *
FROM employee
WHERE EXTRACT(YEAR FROM HIREDATE) NOT IN (1981)*/ 

-- 2.32. List the emps whose Empno not starting with digit78.
/*SELECT * 
FROM employee
WHERE EMPNO NOT LIKE ('78%')*/

-- 2.33. List the emps who are working under ‘MGR’.
/*SELECT CONCAT(E1.ENAME, ' HAS A EMP ', E2.ENAME)
FROM employee AS E1
JOIN employee AS E2 ON E1.MGR = E2.EMPNO*/

-- 2.34. List the emps who joined in any year but not belongs to the month of March.

/*SELECT *
FROM employee
WHERE EXTRACT(MONTH FROM HIREDATE) NOT IN (03)*/ 

-- 2.35. List all the Clerks of Deptno 20.
/*SELECT *
FROM employee
WHERE DEPTNO = 20  AND JOB = 'CLERK'*/ 

-- 2.36. List the emps of Deptno 30 or 10 joined in the year 1981
/*SELECT * 
FROM employee
WHERE DEPTNO IN (30, 10) AND EXTRACT(YEAR FROM HIREDATE) IN (1981)*/ 

-- 2.37. Display the details of SMITH
/*SELECT * 
FROM employee
WHERE ENAME IN ('SMITH')*/ 

-- START OF JOIN 
-- 2.38. Display the location of SMITH.
/*SELECT E.ENAME, D.Location
FROM employee AS E
JOIN dept AS D ON E.DEPTNO = D.DEPTNO
WHERE E.ENAME IN ('SMITH')*/ 

-- 2.39. List the total information of EMP table along with DNAME and Loc of
-- all the emps Working Under ‘ACCOUNTING’ & ‘RESEARCH’ in the asc Deptno.
/* SELECT E.EMPNO, E.ENAME, D.DNAME, D.Location, D.DEPTNO
FROM employee AS E 
JOIN dept AS D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME IN ('accounting', 'research')
ORDER BY E.DEPTNO*/ 

-- 2.40. List the Empno, Ename, Sal, Dname of all the ‘MGRS’ and ‘RESEARCH’
-- working in New York, Dallas with an exp more than 7 years without receiving
-- the Comm asc order of Loc.
/* SELECT E.Empno, E.Ename, E.Salary, D.DNAME,D.Location, E.Comm, E.HIREDATE, TIMESTAMPDIFF(YEAR, E.HIREDATE, CURRENT_DATE) AS YOE
FROM employee AS E 
JOIN dept AS D ON E.DEPTNO = D.DEPTNO
WHERE E.JOB = 'MANAGER' AND D.DNAME = 'RESEARCH' 
AND D.Location IN ('New York', 'Dallas')
AND E.Comm IS NULL 
AND TIMESTAMPDIFF(YEAR, E.HIREDATE, CURRENT_DATE) > 7
ORDER BY D.Location*/ 

-- 2.41. Display the Empno, Ename, Sal, Dname, Loc, Deptno, Job of all emps
-- working at CJICAGO or working for ACCOUNTING dept with Ann Sal>28000, 
-- but the Sal should not be=3000 or 2800 who doesn’t belongs to the
-- Mgr and whose no is having a digit ‘7’ or ‘8’ in 3rd position in the asc order of
-- Deptno and desc order of job.



















 










































 
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;