You are on page 1of 15

CSE 3001-DBMS LABORATORY-EXERCISE SHEET

Experiment No: 2

Experiment Title: Data manipulation Commands Aim:

a) To create a table using Create Command and Display it using DESC command.
b) To insert rows in the created table using INSERT command.
c) Use SELECT command for selection of columns, rows from the table.
d) Usage of SQL clauses such as WHERE, HAVING, GROUP BY, COUNT, DISTINCT, ORDER BY
along with SELECT command.

Procedure:

A) Create Table
SYNTAX: CREATE TABLE table_name( column1
datatype [ NULL | NOT NULL], column2
datatype [ NULL | NOT NULL],
...
);
SQL QUERY: CREATE TABLE customers (
customer_id INT NOT NULL,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
phone VARCHAR (25), email VARCHAR
(255) NOT NULL, street VARCHAR
(255), city VARCHAR (50), state
VARCHAR (25), zip_code VARCHAR (5),
PRIMARY KEY (customer_id)

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

);

B) Insert Row-
SYNTAX: INSERT INTO <table_name> VALUES(<column1>,<column2>,..................);

SQL QUERY:
- INSERT INTO customers VALUES(1,”Debra”,”Burks”,NULL,
“debra.burks@yahoo.com”,”9273 Thome Ave”,”Orchard Park”,”NY”,”14127”);
- INSERT INTO customers VALUES(2,”Kasha”,”Todd”,NULL,
“kasha.todd@yahoo.com”,”910 Vine Street”,”Campbell”,”CA”,”95008”);
- INSERT INTO customers VALUES(3,”Tameka”,”Fisher”,NULL,
“tameka.fisher@aol.com”,”769C Honey Creek St.”,”Redondo Beach”,”CA”,”90278”);
- INSERT INTO customers VALUES(4,”Daryl”,”Spence”,NULL,
“daryl.spence@aol.com”,”988 Pearl Lane”,”Uniondale”,”NY”,”11553”);
- INSERT INTO customers VALUES(5,”Charolette”,”Rice”,”9163816003”,
“charolette.rice@msn.com”,”107 River Dr.”,”Sacramento”,”CA”,”95820”);

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

C) SELECT command:
SYNTAX: SELECT <column1>,<column2>,...........FROM <table_name>;

SQL QUERY: SELECT * FROM customers;

SELECT first_name,last_name from customers;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

D) SQL CLAUSES:
- WHERE clause:

SYNTAX: SELECT <column1>,<column2>,...........FROM <table_name> WHERE <condition>;

SQL QUERY: SELECT * FROM customers WHERE state=”CA”;

- HAVING clause:

SYNTAX: SELECT <column1>,<column2>,...........FROM <table_name> HAVING <condition>;

SQL QUERY: SELECT * FROM customers HAVING state=”CA”;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

- GROUP BY clause:

SYNTAX: SELECT <column1>,<column2>,………… FROM <table_name> GROUP BY


<column_name>;

SQL QUERY: SELECT * FROM customers GROUP BY state;

- COUNT clause:

SYNTAX: SELECT <column1>,<column2>,..........,Count(*) FROM <table_name> GROUP BY


<column_name>;

SQL QUERY: SELECT state,COUNT(*) FROM customers GROUP BY state;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

- DISTINCT clause:

SYNTAX: SELECT <column1>,<column2>,…….,DISTINCT(<column_name>) FROM


<table_name>;

SQL QUERY: SELECT DISTINCT(state) FROM customers;

ORDER BY clause:

SYNTAX: SELECT <column1>,<column2>,<column3>….........FROM <table_name> ORDER BY


<column_name> ;

SQL QUERY: SELECT * FROM customers ORDER BY zip_code;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

E) Using JOIN clauses in SELECT command

1) Create another table – ORDERS

table: QUERY: CREATE TABLE orders(

Order_id INT NOT NULL,

Customer_id INT NOT NULL,

Order_date VARCHAR(10),

State VARCHAR(25),

PRIMARY KEY(customer_id)

);

INSERT INTO orders VALUES(10203, 2, '1996-09-18', 'state2');

INSERT INTO orders VALUES(10013, 3, '1996-07-21', 'state6');

INSERT INTO orders VALUES(10413, 6, '1997-12-01', 'state6');

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

INSERT INTO orders VALUES(10116, 5, '1996-02-22', 'state5');

2) Using the JOIN command on the two tables


a) INNER JOIN Syntax:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column

Query:
SELECT orders.order_id, customers.first_name, orders.order_date FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

b) LEFT JOIN Syntax:


SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column

Query:
SELECT customers.first_name, orders.order_id FROM customers LEFT JOIN orders
ON customers.customer_id = orders.customer_id ORDER BY customers.first_name;

c) RIGHT JOIN Syntax:


SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column

Query:
SELECT customers.first_name, orders.order_id FROM customers RIGHT JOIN
orders ON customers.customer_id = orders.customer_id ORDER BY
customers.first_name;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

d) FULL OUTER JOIN Syntax:


SELECT columns FROM table1 LEFT JOIN table2 on table1.column= table2.column UNION
SELECT columns FROM table1 RIGHT JOIN table2 on table1.column= table2.column;

Query:
SELECT * FROM customers LEFT JOIN orders on customers.customer_id =
orders.customer_id UNION SELECT * FROM customers RIGHT JOIN orders on
customers.customer_id = orders.customer_id;

F) Using UNION, UNION ALL clause with SELECT command

1) UNION command

Syntax:
SELECT columns FROM table1 UNION SELECT columns FROM table2 ORDER BY column;

Query:
SELECT state FROM customers UNION SELECT state FROM orders ORDER BY state;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

2) UNION ALL command

Syntax:
SELECT columns FROM table1 UNION ALL SELECT columns FROM table2 ORDER BY column;

Query:
SELECT state FROM customers UNION ALL SELECT state FROM orders ORDER BY state;

G) Using aggregate functions (COUNT,SUM,AVERAGE,MIN,MAX) with


SELECT command
We create a new table for these functions:

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

1) COUNT function
Syntax:
SELECT COUNT(column) FROM table;

Query:
SELECT COUNT(student_id) FROM student;

2) SUM function

Syntax:
SELECT SUM(column) FROM table;

Query:
SELECT SUM(marks) FROM student;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

3) MAX function

Syntax:
SELECT MAX(column) FROM table;

Query:
SELECT MAX(marks) FROM student;

4) MIN function

Syntax:
SELECT MIN(column) FROM table;

Query:
SELECT MIN(marks) FROM student;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

5) AVERAGE function

Syntax:
SELECT AVG (column) FROM table;

Query:
SELECT AVG (marks) FROM student;

H) Using NOT NULL clause with SELECT command

Syntax:
SELECT column_names FROM table_name WHERE column_name IS NOT NULL;

Query:
SELECT name FROM student WHERE marks IS NOT NULL;

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML
CSE 3001-DBMS LABORATORY-EXERCISE SHEET

Outcome: This experiment taught us how to make use of some of the data manipulation
command in MySQL.

Name of the Student: Soumili Date of Submission: 21/12/2020


Register No/Department: 21BAI10037 BTECH – CSE AI & ML

You might also like