0% found this document useful (0 votes)
15 views43 pages

SQL Practical

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views43 pages

SQL Practical

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 43

SQL LAB MANUAL

DSC 101
FUNDAMENTALS OF SQL

Submitted by

Chirag nagpal

2023431371

MBA – Business Analytics

SEM - III

Batch: 2023-2025

Roll no. 2302040067

Submitted to

Mr. Shashank Bhardwaj

Assistant Professor

DEPARTMENT OF MANAGEMENT

SCHOOL OF BUSINESS STUDIES

SHARDA UNIVERSITY, GREATER NOIDA-201306


SQL Queries

1. USE Clause
Syntax:
USE sql_store;

2. SELECT Statement

Syntax:
SELECT * FROM customers;

3. Using Alias

a) Query to return customer details with new points (points + 120)

Syntax:
SELECT
customer_id,
first_name,
points,
points + 120 AS new_points
FROM
customers;

4. Sorting Data Using ORDER BY

Syntax:

SELECT *
FROM customers
ORDER BY points DESC;
5. Using WHERE Clause
a) Query to fetch customers living in California State

Syntax:
SELECT *
FROM customers
WHERE state = 'California';

6. Using Arithmetic and Logical Operators

a) Query for customers born after 1990 and have more than 1000 points

Syntax:
SELECT *
FROM customers
WHERE YEAR(birth_date) > 1990 AND points > 1000;
b) Query for customers living outside California
Syntax:
SELECT *
FROM customers
WHERE state <> 'California';

7. Using BETWEEN Clause

a) Fetch records of customers born between 01-01-1990 and 01-01-2000

Syntax:
SELECT *
FROM customers
WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01';

8. Using LIKE Operator

a) Query to fetch customers with 'b' in their last name (any position):
Syntax:
SELECT * FROM customers
WHERE last_name LIKE '%b%';

b) Query for customers whose last name ends with 'y':

Syntax:

SELECT *
FROM customers
WHERE last_name LIKE '%y';

9. Using REGEXP Clause

a) Query to search customers with 'field' or 'mac' in their last name:


Syntax:

SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac';

a) i. Customers with first names 'Elka' or 'Ambur':

Syntax:

SELECT *
FROM customers
WHERE first_name IN ('Elka', 'Ambur');
b) ii. Customers whose last name ends with 'ey' or 'on':

Syntax:
SELECT *
FROM customers
WHERE last_name REGEXP 'ey$|on$';

b) iii. Customers whose last starts with ‘My’ or contains ‘se’:

Syntax:
SELECT *
FROM customers
WHERE last_name REGEXP ‘^My|se’;

b) iv. Customers whose last name contains ‘b’ followed by ‘r’ or ‘u’:

Syntax:

SELECT *
FROM customers
WHERE last_name REGEXP 'b[r|u]';
10. IS NULL Operator
a) Fetch orders that are not shipped:
Syntax:
SELECT *
FROM orders
WHERE shipped_date IS NULL;

11. LIMIT Clause

a) Sort customers table by state and then by first name:

Syntax:

SELECT*

FROM

ORDER BY state, first_name;


b. Fetch only first_name and last_name, sorted by birth_date:

Syntax:

SELECT first_name, last_name


FROM customers
ORDER BY birth_date;
c) Fetch records from order_items where order_id = 2, sorted by quantity * unit_price in
descending order:

Syntax:

SELECT *
FROM order_items
WHERE order_id = 2
ORDER BY quantity * unit_price DESC;

12. INNER JOIN

a) Query to join order_items with products:


i. For each order, return product_id, name, quantity, and unit_price:

Syntax:

SELECT
order_items.product_id,
products.name,
order_items.quantity,
order_items.unit_price
FROM
order_items
INNER JOIN
products ON order_items.product_id = products.product_id;
13. SELF JOIN

Syntax:

SELECT e1.first_name AS Employee1, e2.first_name AS Employee2


FROM employees e1
INNER JOIN employees e2 ON e1.manager_id = e2.employee_id;
14. CREATE DATABASE and TABLE

a. Query to create a students table:

Syntax:

CREATE DATABASE school;

USE school;

CREATE TABLE students (


student_id INT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
age INT,
email VARCHAR(255) UNIQUE
);
15. DROP DATABASE and TABLE

Syntax:

DROP DATABASE sql_store;


DROP TABLE students;

16. Using INSERT INTO Statement

Syntax:

INSERT INTO students (student_id, name, age, email)


VALUES (1, 'John Doe', 21, 'john.doe@example.com');
17. Using ALTER TABLE Statement

Syntax:

ALTER TABLE student


ADD phone_number VARCHAR(15);
ASSIGNMENT 1
1. Retrieve Customer Information: Write a query to retrieve the
first_name, last_name, and city of customers who live in
either "Chicago", "Orlando", or "Nashville".

Syntax:
SELECT
first_name,
last_name,
city
FROM
customers
WHERE city = 'Chicago’ OR 'Orlando' OR 'Nashville’;
2. Orders from a Specific Date Range: Write a query to find all
orders placed between 2018-01-01 and 2018-12-31, and sort the
results by the order_date in descending order.

Syntax:
SELECT * FROM orders
WHERE order_date BETWEEN '2018-01-01' AND '2018-12-31'
ORDER BY order_date DESC;

3. Search for Customers by Phone: Write a query to find customers


whose phone number starts with "781".

Syntax:
SELECT * FROM customers
WHERE phone LIKE '781%';
4. Products with Specific Unit Price: Write a query to retrieve
the name and unit_price of products where the unit_price is
greater than 3.00.

Syntax:
SELECT name, unit_price FROM products
WHERE unit_price > 3.00;
5. Find Expensive Orders: Write a query to find order_id and
product_id for orders where the total price exceeds 50.00.

Syntax:
SELECT * , quantity*unit_price AS total_price FROM order_items
WHERE quantity * unit_price > 50.00;

6. Customers with Null Values: Write a query to find customers


who have NULL values in the phone.

Syntax:
SELECT * FROM customers
WHERE phone IS NULL;
7. Order Status Search: Write a query to find all orders that
have a status of either 1 or 2.

Syntax:
SELECT * FROM orders
WHERE status IN (1, 2);

8. Retrieve Specific Customers: Write a query to find customers


whose first_name starts with either "A" or "I".

Syntax:
SELECT * FROM customers
WHERE first_name LIKE 'A%' OR first_name LIKE 'I%';
9. Top 5 Customers with Highest Points: Write a query to retrieve
the top 5 customers with the highest points.

Syntax:
SELECT * FROM customers
ORDER BY points DESC
LIMIT 5;
10. Products with Low Stock: Write a query to find the name and
quantity_in_stock of products where the quantity_in_stock is
less than or equal to 10.

Syntax:
SELECT name,quantity_in_stock
FROM products
WHERE quantity_in_stock <= 10;

11. Orders with Comments Containing Specific Words: Write a query


to find all orders where the comments field contains the word
"volutpat".

Syntax:
SELECT * FROM orders
WHERE comments LIKE '%volutpat%';
12. Write a query to find all products with a unit_price between
3.00 and 6.00 (inclusive). Sort the results by unit_price in
ascending order.

Syntax:
SELECT * FROM products
WHERE unit_price BETWEEN 3.00 AND 6.00
ORDER BY unit_price ASC;
ASSIGNMENT 2
Q.1. Write a query to fetch "first_name" from Customers table using
the alias name <Customer_Name>.

Syntax:
SELECT first_name AS customer_Name
FROM customers;

Q.2. Write a query to fetch "first_name" from Customers table in upper


case

Syntax:
SELECT UPPER(first_name)
FROM customers;
Q.3. Write a query to fetch unique values of "State" from Customers
table

Syntax:
SELECT DISTINCT state
FROM customers;
Q.4. Write a query to print the first three characters of "first_name"
from Customers table

Syntax:
SELECT LEFT(first_name, 3) AS first_three_char
FROM customers;

Q.5. Write a query to fetch the unique value of "State" from Customers
table and print its length

Syntax:
SELECT DISTINCT state, LENGTH(State) AS state_length
FROM customers;
Q.6. Write a query to print the "first_name" and "last_name" from the
Customers table into a single column "Complete_Name". A space bar
should separate them.

Syntax:
SELECT CONCAT(first_name, ' ', last_name) AS complete_name
FROM customers;
Q.7. Write a query to print details for Customers with first names
"Ambur" and "Levy" from the Customers table.

Syntax:
SELECT * FROM customers
WHERE first_name IN ('Ambur', 'Levy');

Q.8. Write a query to print details for Customers excluding first


names "Ambur" and "Levy" from Customers table.

Syntax:
SELECT * FROM customers
WHERE first_name NOT IN ('Ambur', 'Levy');
Q.9. Write a query to print details of Customers with word "Trail" in
their address

Syntax:
SELECT * FROM customers
WHERE address LIKE '%Trail%';
Q.10. Write a query to print details of a Customer whose first_name
ends with 'r' and contains 5 characters.

Syntax:
SELECT * FROM customers
WHERE first_name LIKE '____r';
ASSIGNMENT 3

Q.1 Create a new database “Sales_dept” and create new tables as given
above along with the data as shown.

Syntax:
CREATE DATABASE Sales_dept;
USE Sales_dept;

Syntax:
CREATE TABLE salesman ( salesman_id INT PRIMARY KEY, name VARCHAR(50),
city VARCHAR(50), commission DECIMAL(5, 2) );
INSERT INTO salesman
VALUES (5001, 'James Hoog', 'New York', 0.15),
(5002, 'Nail Knite', 'Paris', 0.13),
(5005, 'Pit Alex', 'London', 0.11),
(5006, 'Mc Lyon', 'Paris', 0.14),
(5007, 'Paul Adam', 'Rome', 0.13),
(5003, 'Lauson Hen', 'San Jose', 0.12);
Syntax:
CREATE TABLE customer ( customer_id INT PRIMARY KEY, cust_name
VARCHAR(50), city VARCHAR(50), grade INT, salesman_id INT );
INSERT INTO customer
VALUES (3002, 'Nick Rimando', 'New York', 100, 5001),
(3007, 'Brad Davis', 'New York', 200, 5001),
(3005, 'Graham Zusi', 'California', 200, 5002),
(3008, 'Julian Green', 'London', 300, 5002),
(3004, 'Fabian Johnson', 'Paris', 300, 5006),
(3009, 'Geoff Cameron', 'Berlin', 100, 5003),
(3003, 'Jozy Altidor', 'Moscow', 200, 5007),
(3001, 'Brad Guzan', 'London', NULL, 5005);
Syntax:
CREATE TABLE orders ( ord_no INT PRIMARY KEY, purch_amt DECIMAL(10,
2), ord_date DATE, customer_id INT, salesman_id INT );
INSERT INTO orders
VALUES (70001, 150.5, '2012-10-05', 3005, 5002),
(70009, 270.65, '2012-09-10', 3001, 5005),
(70002, 65.26, '2012-10-05', 3002, 5001),
(70004, 110.5, '2012-08-17', 3009, 5003),
(70007, 948.5, '2012-09-10', 3005, 5002),
(70005, 2400.6, '2012-07-27', 3007, 5001),
(70008, 5760, '2012-09-10', 3002, 5001),
(70010, 1983.43, '2012-10-10', 3004, 5006),
(70003, 2480.4, '2012-10-10', 3009, 5003),

(70012, 250.45, '2012-06-27', 3008, 5002),


(70011, 75.29, '2012-08-17', 3003, 5007),
(70013, 3045.6, '2012-04-25', 3002, 5001);
Q 2 Write a query to find those customers with their name and those
salesmen with their name and city who live in the same city.

Syntax:

SELECT
customer.cust_name AS customer_name,
salesman.name AS salesman_name,
salesman.city
FROM
customer
JOIN
salesman
ON
customer.city = salesman.city;
Q 3 Write a SQL statement to find the names of all customers along
with the salesmen who work for them.

Syntax:
SELECT
customer.cust_name AS customer_name,
salesman.name AS salesman_name
FROM
customer
JOIN
salesman
ON
customer.salesman_id = salesman.salesman_id;
Q.4 Write a SQL statement to display all those orders by the customers
not located in the same cities where their salesmen live.

Syntax:
SELECT
orders.ord_no,
customer.cust_name,
salesman.name AS salesman_name,
customer.city AS customer_city,
salesman.city AS salesman_city
FROM
orders
JOIN
customer
ON
orders.customer_id = customer.customer_id
JOIN
salesman
ON
orders.salesman_id = salesman.salesman_id
WHERE
customer.city <> salesman.city;
Q 5 Write a SQL statement that finds out each order number followed by
the name of the customers who made the order.

Syntax:
SELECT
orders.ord_no,
customer.cust_name
FROM
orders
JOIN
customer
ON
orders.customer_id = customer.customer_id;
Q 6 Write a SQL statement that sorts out the customer and their grade
who made an order. Each of the customers must have a grade and be
served by at least a salesman, who belongs to a city.

Syntax:
SELECT
customer.cust_name,
customer.grade,
salesman.name AS salesman_name,
salesman.city AS salesman_city
FROM
customer
JOIN
salesman
ON
customer.salesman_id = salesman.salesman_id
WHERE
customer.grade IS NOT NULL;
ASSIGNMENT 4
1.Using sql_hr Database - employees table and offices table
write a query to fetch the record of employees and the address of the
office they work in as follows:

employee_id first_name last_name office_id office_address

Syntax:
SELECT
employees.employee_id,
employees.first_name,
employees.last_name,
employees.office_id,
offices.address AS office_address
FROM
employees
JOIN
offices
ON
employees.office_id = offices.office_id;
2. Using sql_invoiving Database, clients table and invoices table,
write a query to fetch the records as given below to show the invoice
total against the order placed by client

client_id client_name invoice_id onvoice_total

Syntax:
SELECT
employees.employee_id,
employees.first_name,
employees.last_name,
employees.office_id,
offices.address AS office_address
FROM
employees
JOIN
offices
ON
employees.office_id = offices.office_id;

You might also like