0% found this document useful (0 votes)
4 views3 pages

All Quries of Selected Labs With Skip Topics

The document outlines various SQL commands and queries used in labs for managing and retrieving data from a MySQL database. It includes instructions for creating and modifying tables, performing selections, joins, and sub-queries to analyze employee data. Each command is accompanied by a brief description of its purpose and functionality.

Uploaded by

f23mmg01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

All Quries of Selected Labs With Skip Topics

The document outlines various SQL commands and queries used in labs for managing and retrieving data from a MySQL database. It includes instructions for creating and modifying tables, performing selections, joins, and sub-queries to analyze employee data. Each command is accompanied by a brief description of its purpose and functionality.

Uploaded by

f23mmg01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 3

Query Description
mysql -u root -p Connect to MySQL from command line.
CREATE TABLE Students (RollNo INT
PRIMARY KEY, Name VARCHAR(50), Age INT, Create a student table.
Department VARCHAR(50));
ALTER TABLE Students ADD Email
VARCHAR(50); Add an Email column.
ALTER TABLE Students MODIFY Age
SMALLINT; Change Age column type.

ALTER TABLE Students DROP COLUMN Email; Remove Email column.


ALTER TABLE Students RENAME TO
University_Students; Rename Students table.
TRUNCATE TABLE University_Students; Delete all records, keep structure.
DESC University_Students; Show table structure.
GRANT SELECT, UPDATE ON Students TO
user; Give user select & update access.

REVOKE UPDATE ON Students FROM user; Remove update access from user.
SHOW DATABASES; List all databases.
SOURCE hr_schema.sql; Import HR schema.

Lab 4
Query Description
SELECT first_name AS 'First Name', last_name
AS 'Last Name' FROM employees; Display names with alias.
SELECT DISTINCT department_id FROM employees; Get unique department IDs.
SELECT * FROM employees ORDER BY first_name
DESC; List employees in descending order.
SELECT employee_id, first_name, salary FROM
employees ORDER BY salary ASC; List employees sorted by salary.
SELECT SUM(salary) FROM employees; Total salary expense.
SELECT MAX(salary), MIN(salary) FROM
employees; Get highest & lowest salary.
SELECT AVG(salary), COUNT(*) FROM employees; Average salary & employee count.
SELECT COUNT(DISTINCT job_id) FROM employees; Count unique jobs.
SELECT UPPER(first_name) FROM employees; Convert names to uppercase.
SELECT * FROM employees LIMIT 10; Get first 10 records.
SELECT * FROM employees LIMIT 2 OFFSET 2; Get 3rd & 4th records.
SELECT * FROM employees ORDER BY employee_id
DESC LIMIT 1 OFFSET 1; Get 2nd last record.
Lab 5

Query Description
SELECT * FROM employees WHERE salary < 3000;
Find employees earning below
$3000.
SELECT * FROM employees WHERE first_name LIKE 'A%'; Find names starting with 'A'.
SELECT * FROM employees WHERE job_id = 'PU_CLERK' OR
manager_id = 114; Find specific employees.
SELECT * FROM employees WHERE salary BETWEEN 1500 AND
3000; Find employees in salary range.

SELECT * FROM employees WHERE commission_pct IS NULL;


Find employees without
commission.
SELECT * FROM employees WHERE first_name LIKE '%N'; Find names ending with 'N'.
SELECT * FROM employees WHERE job_id != 'PU_CLERK'; Exclude PU_CLERK employees.
SELECT * FROM employees WHERE salary NOT IN (3300,
3200, 2200); Exclude specific salaries.

SELECT * FROM employees WHERE first_name LIKE 'A%N';


Names starting with 'A' and ending
with 'N'.
SELECT * FROM employees WHERE first_name LIKE '%LA%'; Names containing 'LA'.

Lab 06: SQL Joins


Query Description
SELECT e.first_name, e.last_name, d.department_name FROM employees Get employee
e JOIN departments d ON e.department_id = d.department_id;
& department
details.

SELECT e.first_name, e.last_name, d.department_name FROM employees Employees


e JOIN departments d ON e.department_id = d.department_id JOIN
working in
locations l ON d.location_id = l.location_id WHERE l.city =
'London'; London.

SELECT e.employee_id, e.last_name, m.last_name AS Manager FROM Employee-


employees e JOIN employees m ON e.manager_id = m.employee_id;
manager
relationships.

SELECT department_id, COUNT(*) FROM employees GROUP BY Count


department_id;
employees per
department.
SELECT d.department_name, m.first_name FROM employees m JOIN Get department
departments d ON m.department_id = d.department_id WHERE m.job_id =
managers.
'Manager';
Lab 07: SQL Sub-Queries
Query Description
SELECT e.employee_id, e.first_name, e.last_name, Get employee details using sub-query.
(SELECT d.department_name FROM departments d WHERE
d.department_id = e.department_id) AS
department_name FROM employees e;

SELECT first_name, last_name FROM employees WHERE Find employee with highest salary.
salary = (SELECT MAX(salary) FROM employees);

SELECT first_name, last_name, salary FROM employees Find employees earning above average
WHERE salary > (SELECT AVG(salary) FROM employees);
salary.

SELECT * FROM employees WHERE department_id IN Get employees in IT department.


(SELECT department_id FROM departments WHERE
department_name = 'IT');

SELECT first_name, last_name FROM employees WHERE Find employees earning more than at
salary > ANY (SELECT salary FROM employees WHERE
least one employee in department 30.
department_id = 30);

SELECT first_name, last_name FROM employees WHERE Find employees earning more than all
salary > ALL (SELECT salary FROM employees WHERE
department_id = 30);
employees in department 30.

SELECT department_name FROM departments WHERE EXISTS Get departments that have employees.
(SELECT 1 FROM employees WHERE
employees.department_id =
departments.department_id);

SELECT department_name FROM departments WHERE NOT Get departments with no employees.
EXISTS (SELECT 1 FROM employees WHERE
employees.department_id =
departments.department_id);

SELECT employee_id, first_name, last_name FROM Find employees who are managers.
employees WHERE EXISTS (SELECT * FROM employees
WHERE manager_id = employees.employee_id);

SELECT first_name, last_name, salary FROM employees Employees earning more than highest
WHERE salary > (SELECT MAX(AVG_salary) FROM (SELECT
department average.
AVG(salary) AS AVG_salary FROM employees GROUP BY
department_id) AS dept_avg_salary);

You might also like