Create table countries CREATE TABLE countries ( country_id INT PRIMARY KEY, country_name VARCHAR(50) ); -- Insert some sample data into the countries table INSERT INTO countries (country_id, country_name) VALUES (1, 'United Kingdom'), (2, 'United States'), (3, 'France'); -- Create table locations CREATE TABLE locations ( location_id INT PRIMARY KEY, city VARCHAR(50), country_id INT, FOREIGN KEY (country_id) REFERENCES countries(country_id) ); -- Insert some sample data into the locations table INSERT INTO locations (location_id, city, country_id) VALUES (1, 'London', 1), (2, 'New York', 2), (3, 'Paris', 3); -- Create table department CREATE TABLE department ( department_id INT PRIMARY KEY, department_name VARCHAR(50), location_id INT, FOREIGN KEY (location_id) REFERENCES locations(location_id) ); -- Insert some sample data into the department table INSERT INTO department (department_id, department_name, location_id) VALUES (1, 'Engineering', 1), (2, 'Marketing', 2), (3, 'Sales', 3); -- Create table job CREATE TABLE job ( job_id INT PRIMARY KEY, job_title VARCHAR(50) ); -- Insert some sample data into the job table INSERT INTO job (job_id, job_title) VALUES (1, 'Manager'), (2, 'Developer'), (3, 'Analyst'), (4, 'Designer'); -- Create table employee CREATE TABLE employee ( employee_number INT PRIMARY KEY, employee_name VARCHAR(50), job_title VARCHAR(50), salary DECIMAL(10, 2), department_id INT, FOREIGN KEY (department_id) REFERENCES department(department_id) ); -- Insert some sample data into the employee table INSERT INTO employee (employee_number, employee_name, job_title, salary, department_id) VALUES (1, 'John Doe', 'Manager', 60000.00, 1), (2, 'Jane Smith', 'Developer', 50000.00, 1), (3, 'Michael Johnson', 'Analyst', 45000.00, 2), (4, 'Emily Brown', 'Designer', 55000.00, 2), (5, 'David Lee', 'Manager', 62000.00, 3), (6, 'Sarah Wilson', 'Developer', 51000.00, 3); SELECT e.employee_number, e.employee_name, e.job_title FROM employee e JOIN ( SELECT department_id, AVG(salary) AS avg_salary FROM employee GROUP BY department_id ) AS dept_avg ON e.department_id = dept_avg.department_id WHERE e.salary > dept_avg.avg_salary; 2. SELECT e.employee_number, e.employee_name, e.job_title FROM employee e JOIN department d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id WHERE l.city = 'London'; 3. SELECT e.employee_name FROM employee e JOIN ( SELECT department_id, SUM(salary) AS total_salary FROM employee GROUP BY department_id ) AS dept_total ON e.department_id = dept_total.department_id WHERE e.salary > 0.5 * dept_total.total_salary; 4. SELECT e.employee_name, e.salary, e.department_id FROM employee e JOIN ( SELECT department_id, MIN(salary) AS min_salary FROM employee GROUP BY department_id ) AS dept_min ON e.department_id = dept_min.department_id WHERE e.salary < dept_min.min_salary AND e.department_id = 70;
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;