Sample Table: employees
employee_id first_name last_name department salary
1 John Doe Sales 60000
2 Jane Smith Marketing 75000
3 Alice Johnson Sales 65000
4 Bob Brown HR 70000
5 Charlie Davis IT 80000
SQL Commands with Examples
1. SELECT
Retrieves data from the employees table.
Get all columns:
SELECT * FROM employees;
Get specific columns:
SELECT first_name, last_name FROM employees;
2. INSERT
Adds a new row to the employees table.
Insert a new employee:
INSERT INTO employees (employee_id, first_name, last_name,
department, salary)
VALUES (6, 'Eve', 'White', 'Marketing', 72000);
3. UPDATE
Modifies existing data in the employees table.
Update salary for an employee:
UPDATE employees
SET salary = 68000
WHERE employee_id = 3;
4. DELETE
Removes rows from the employees table.
Delete an employee record:
DELETE FROM employees
WHERE employee_id = 1;
5. CREATE TABLE
Creates a new table.
Create a new departments table:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
6. ALTER TABLE
Modifies the structure of an existing table.
Add a new column to employees:
ALTER TABLE employees
ADD hire_date DATE;
7. DROP TABLE
Deletes a table and all of its data.
Drop the departments table:
DROP TABLE departments;
8. GRANT
Gives a user permission to perform actions.
Grant SELECT permission to a user:
GRANT SELECT ON employees TO user_name;
9. REVOKE
Removes permissions from a user.
Revoke SELECT permission from a user:
REVOKE SELECT ON employees FROM user_name;
10. COMMIT and ROLLBACK
Manage transactions.
Start a transaction, update, and commit:
BEGIN;
UPDATE employees SET salary = 70000 WHERE employee_id = 2;
COMMIT;
Rollback if something goes wrong:
BEGIN;
UPDATE employees SET salary = 70000 WHERE employee_id = 2;
ROLLBACK; -- This undoes the update if executed.
Additional Clauses
11. WHERE
Filters results based on a condition.
Get employees in the Sales department:
SELECT * FROM employees WHERE department = 'Sales';
12. ORDER BY
Sorts the result set.
Get all employees sorted by salary in descending order:
SELECT * FROM employees ORDER BY salary DESC;
13. GROUP BY
Groups rows that have the same values.
Count employees by department:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
14. HAVING
Filters groups based on aggregate conditions.
Get departments with more than one employee:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;
ASSIGNMENT 3
Table for CLIENT_MASTER
CREATE TABLE CLIENT_MASTER ( CLIENT_NO VARCHAR2(6), CLIENT_NAME VARCHAR2(20),
CITY VARCHAR2(15), STATE VARCHAR2(15), PIN_CODE VARCHAR2(6), BALANCE_DUE
NUMBER(10, 2) );
Insert Data
INSERT INTO CLIENT_MASTER VALUES ('0001', 'Ivan', 'Bombay', '400057', 'Maharastra',
15000);
INSERT INTO CLIENT_MASTER VALUES ('0002', 'Vandura', 'Madras', '980001', 'Tamilnadu', 0);
INSERT INTO CLIENT_MASTER VALUES ('0003', 'Pramod', 'Bombay', '400057', 'Maharastra',
5000);
INSERT INTO CLIENT_MASTER VALUES ('0004', 'Basu', 'Bombay', '400056', 'Maharastra', 0);
INSERT INTO CLIENT_MASTER VALUES ('0005', 'Ravi', 'Delhi', '100001', NULL, 2000); INSERT
INTO CLIENT_MASTER VALUES ('0006', 'Rukmini', 'Bombay', '900050', 'Maharastra', 0);
SELECT * FROM CLIENT_MASTER;
Table for PRODUCT_MASTER
CREATE TABLE PRODUCT_MASTER ( PRODUCT_NO VARCHAR2(6), DESCRIPTION
VARCHAR2(20), PROFIT_PERCENT NUMBER(10, 2), QTY_ON_HAND NUMBER(10),
ORDER_LEVEL NUMBER(10), SELL_PRICE NUMBER(10), COST_PRICE NUMBER(10) );
Insert Data
INSERT INTO PRODUCT_MASTER VALUES ('P00001', '1.44 floppies', 5, 100, 20, 525, 500);
INSERT INTO PRODUCT_MASTER VALUES ('P03453', 'Monitors', 6, 10, 3, 12000, 11200);
INSERT INTO PRODUCT_MASTER VALUES ('P06734', 'Mouse', 5, 20, 5, 1050, 500);
INSERT INTO PRODUCT_MASTER VALUES('P07865', '1.22 floppies', 5, 100, 20, 525, 500);
INSERT INTO PRODUCT_MASTER VALUES ('P07868', 'Keyboards', 2, 10, 3, 3150, 3050);
INSERT INTO PRODUCT_MASTER VALUES ('P07885', 'CD drive', 2.5, 10, 3, 5250, 5100);
INSERT INTO PRODUCT_MASTER VALUES ('P07965', '540 HDD', 4, 10, 3, 8400, 8000);
INSERT INTO PRODUCT_MASTER VALUES ('P07975', '1.44 Drive', 5, 10, 3, 1050, 1000);
INSERT INTO PRODUCT_MASTER VALUES ('P08865', '1.22 Drive', 5, 2, 3, 1050, 1000);
SELECT * FROM PRODUCT_MASTER;
(a) Delete the record of Client no. 0001 from the CLIENT_MASTER table:
DELETE FROM CLIENT_MASTER
WHERE CLIENT_NO = '0001';
(b) Change the city of Client no. 0005 to ‘Bombay’:
UPDATE CLIENT_MASTER
SET CITY = 'Bombay'
WHERE CLIENT_NO = '0005';
(c) Change the balance due of Client no. 0002 to 1000:
UPDATE CLIENT_MASTER
SET BALANCE_DUE = 1000
WHERE CLIENT_NO = '0002';
(d) Find out the clients who stay in a city or state where the second letter is 'a':
SELECT * FROM CLIENT_MASTER
WHERE CITY LIKE '_a%' OR STATE LIKE '_a%';
(e) Calculate the average balance due of all the clients:
SELECT AVG(BALANCE_DUE) AS AVERAGE_BALANCE_DUE
FROM CLIENT_MASTER;
(f) Change the selling price of the 1.44 floppy drive to Rs. 1150.00:
UPDATE PRODUCT_MASTER
SET SELL_PRICE = 1150
WHERE DESCRIPTION = '1.44 Drive';
(g) Count the number of products having a selling price greater than or equal
to 1500:
SELECT COUNT(*) AS PRODUCT_COUNT
FROM PRODUCT_MASTER
WHERE SELL_PRICE >= 1500;