CREATE DATABASE sample;
USE sample;
SHOW TABLES
CREATE TABLE employee
(
id INT PRIMARY KEY,
age INT,
name VARCHAR(30),
dob DATETIME,
email VARCHAR(50),
department VARCHAR(10),
salary DECIMAL(10.2) --two digits after decimal point
);
DESC employee;
INSERT INTO employee(id, age, name, dob, email, department, salary)
VALUES (1121, 22, 'SayantanRC', '1996-06-25 17:33:21', '[email protected]', 'android', 52000.50);
INSERT INTO employee
VALUES (1122, 23, 'ABC', '1996-06-26 15:53:51', '[email protected]', 'design', 55000.00);
INSERT INTO employee
VALUES (1123, 22, 'BCD', '1997-06-26 15:50:11', '[email protected]', 'design', 50000.00);
INSERT INTO employee
VALUES (1124, 23, 'ArupG', '1989-01-26 10:40:00', '[email protected]', 'fabrication', 56500.00);
CREATE TABLE emp_info AS
SELECT id, dob, email
FROM employee
WHERE department='design';
INSERT INTO emp_info(id, dob, email)
SELECT id, dob, email
FROM employee
WHERE email NOT NULL;
SELECT id, name
FROM employee
SELECT 5 + 10;
SELECT 11 % 5;
SELECT *
FROM employee
WHERE salary>53000;
SELECT id
FROM employee
WHERE department<>'design'; --not equal to
SELECT *
FROM employee
WHERE salary>53000 AND department='design';
SELECT *
FROM employee
WHERE name LIKE 'S%' OR name LIKE '_B_'; --% means one, zero or more chars, _ means one char
SELECT *
FROM employee
WHERE dapartment IN ('design', 'fabrication', 'paint'); --Skips the non existent 'paint' department.
SELECT *
FROM employee
WHERE salary BETWEEN 50000 AND 55000; --both limits are included
SELECT *
FROM employee
WHERE id >= 1122 ALL (SELECT id FROM employee WHERE salary < 56000); --shows the middle two rows
UPDATE employee
SET email='[email protected]'
WHERE name='SayantanRC'
SELECT TOP 3 *
FROM employee;
SELECT *
FROM employee
LIMIT 3;
SELECT *
FROM employee
ORDER BY name; --ascending
SELECT *
FROM employee
ORDER BY salary DESC; --descending
SELECT age, COUNT(*) --this query returns 2 columns, age and number of people with that age
FROM employee
GROUP BY age;
SELECT age, SUM(salary) --this query returns 2 columns, age and total salary of that age group
FROM employee
GROUP BY age;
SELECT DISTINCT age
FROM employee
Make a new table first:
CREATE TABLE compensation
(
department VARCHAR(10) PRIMARY KEY,
amount DECIMAL(5.2)
);
Insert into table:
INSERT INTO compensation
VALUES ('android', 5000);
INSERT INTO compensation
VALUES ('design', 4500.50);
INSERT INTO compensation
VALUES ('paint', 5200.00);
Make a JOIN statement to join the two tables.
SELECT e.id, e.name, e.salary, c.amount
FROM employee AS e
JOIN compensation AS c
ON e.department=c.department;
Or
SELECT e.id, e.name, e.salary, c.amount
FROM employee AS e
JOIN compensation AS c
ON e.department=c.department;
compensation table has no row with department
='fabrication'. So, based on the given ON condition, employee 'ArupG' will not be shown because for him, e.departent
is never equal to c.department
.
This will show employee 'ArupG' with a amount
=null from compensation table.
SELECT e.id, e.name, e.salary, c.amount
FROM employee AS e
LEFT JOIN compensation AS c
ON e.department=c.department;
This will show an employee with e.id
=e.name
=e.salary
=null and amount
=5200.00 (for department
='paint') from compensation table.
SELECT e.id, e.name, e.salary, c.amount
FROM employee AS e
RIGHT JOIN compensation AS c
ON e.department=c.department;
DELETE FROM employee
WHERE id=1123
DROP TABLE emp_info;
DROP DATABASE sample;