SQL Practical
SQL Practical
DSC 101
FUNDAMENTALS OF SQL
Submitted by
Chirag nagpal
2023431371
SEM - III
Batch: 2023-2025
Submitted to
Assistant Professor
DEPARTMENT OF MANAGEMENT
1. USE Clause
Syntax:
USE sql_store;
2. SELECT Statement
Syntax:
SELECT * FROM customers;
3. Using Alias
Syntax:
SELECT
customer_id,
first_name,
points,
points + 120 AS new_points
FROM
customers;
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';
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';
Syntax:
SELECT *
FROM customers
WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01';
a) Query to fetch customers with 'b' in their last name (any position):
Syntax:
SELECT * FROM customers
WHERE last_name LIKE '%b%';
Syntax:
SELECT *
FROM customers
WHERE last_name LIKE '%y';
SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac';
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$';
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;
Syntax:
SELECT*
FROM
Syntax:
Syntax:
SELECT *
FROM order_items
WHERE order_id = 2
ORDER BY quantity * unit_price DESC;
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:
Syntax:
USE school;
Syntax:
Syntax:
Syntax:
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;
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;
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);
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;
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;
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');
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),
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:
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
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;