SQL Lab Assign
SQL Lab Assign
Why SQL?
Allows users to access data in relational database management systems.
Allows users to define the data in database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE,
DELETE and DROP. These commands can be classified into groups based on their nature:
DDL -Data Definition Language:
Command Description
CREATE Creates a new table, a view of a
table, or other object in database
SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
SQL Operators:
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as comparisons
and arithmetic operations.
Operators are used to specify conditions in an SQL statement and to serve as
conjunctions for multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
Syntax:
The basic syntax for creating a table from another table is as follows:
Here, column1, column2...are the fields of existing table and same would be used to create
fields of new table.
Example:
Following is an example, which would create a table SALARY using CUSTOMERS table
and having fields customer ID and customer SALARY:
SQL> CREATE TABLE SALARY AS
SELECT ID, SALARY
FROM CUSTOMERS;
ASSIGNMENT 3
Example:
Following statements would create six records in CUSTOMERS table:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 );
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Komal', 22, 'MP', 4500.00 );
All the above statements would produce the following records in CUSTOMERS table:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Syntax:
The basic syntax of SELECT statement is as follows:
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2...are the fields of a table whose values you want to fetch. If you want to fetch all the fields available
in the field, then you can use the following syntax:
SELECT * FROM table_name;
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields of the customers available in CUSTOMERS table:
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
If you want to fetch all the fields of CUSTOMERS table, then use the following query:
SQL> SELECT * FROM CUSTOMERS;
This would produce the following result:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
DDL QUERIES:
Q1. Write a query to create a table employee with empno, ename, designation, andsalary.
SQL>CREATE TABLE EMP (EMPNO NUMBER (4),
ENAME VARCHAR2 (10),
DESIGNATIN VARCHAR2 (10),
SALARY NUMBER (8,2));
Table created.
Q2. Write a query for create a from an existing table with all the fields.
SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;
Table created.
SQL> DESC EMP1
Name Null? Type
----------------------------------------- -------- ------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (10)
DESIGNATIN VARCHAR2 (10)
SALARY NUMBER (8,2)
Q3. Write a Query to Alter the column EMPNO NUMBER(4) TO EMPNO NUMBER(6).
SQL>ALTER TABLE EMP MODIFY EMPNO NUMBER (6);
Table altered.
Q4. Write a query to add a new column in to employee.
SQL> ALTER TABLE EMP ADD QUALIFICATION VARCHAR2(6);
Table altered.
Q5. Write a query to drop a column from an existing table employee.
SQL> ALTER TABLE EMP DROP COLUMN DOJ;
Table altered.
Q6. Write a query to drop an existing table employee.
SQL> DROP table employee;
Table deleted.
DML QUERIES:
Q1. Write a query to insert the records in to employee.
SQL>INSERT INTO EMP VALUES(103,'Saurabh','ASST_PROF',25000);
1 row created.
Q2. Write a query to display the records from employee.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ---------- ----------
103 SAURABH ASST_PROF 25000
Q3. Write a query to insert the records in to employee using substitution
method.
SQL> INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY');
Enter value for empno: 102
Enter value for ename: DHAJVEER
Enter value for designatin: ASST_PROF
Enter value for salary: 35000
old 1: INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP
VALUES(102,'DHAJVEER','ASST_PROF','35000')
1 row created.
SQL> /
Enter value for empno: 101
Enter value for ename: ABHILASHA
Enter value for designatin: ASST_PROF
Enter value for salary: 40000
old 1: INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP
VALUES(101,'ABHILASHA','ASST_PROF','40000')
1 row created.
Q4. Write a query to update the records from employee.
SQL> UPDATE EMP SET SALARY=45000 WHERE EMPNO=101;
1 row updated.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ ---------- ----------
101 ABHILASHA ASST_PROF 45000
102 DHAJVEER ASST_PROF 35000
103 SAURABH ASST_PROF 30000
Outcome:
Assignment 6
To understand the basic commands of DML and DDL and their use in database.
Pre-Requisite Data:
CUSTO NAME AGE ADDRE SALAR
MER SS Y
TABLE
ID
1 Akshay 25 Delhi 30000
2 Manish 27 Mumbai 35000
3 Kushagr 26 Kolkata 30000
a
4 Mukesh 31 Hyderab 32000
ad
5 Himans 29 Chennai 40000
hu
6 Neeraj 30 Noida 36000
7 Nishant 32 Delhi 30000
Q2. Write a query to find the salary of a person where age is <= 26 or salary > =33000 from customer
table.
SQL>SELECT * FROM CUSTOMERS WHERE AGE <= 26 or SALARY > =33000;
5 rows selected.
Q3.Write a query to find the name of customer whose name is like “Ku%”.
SQL>SELECT * FROM CUSTOMERS WHERE NAME LIKE 'Ku%';
Output: NAME AGE ADDRE SALAR
ID SS Y
3 Kushagr 26 Kolkata 30000
a
1 row selected.
Q4. Write a query to find the customer details using “IN” and “Between” operator where age can
be 25 or 27.
SQL>SELECT * FROM CUSTOMERS WHERE AGE IN ( 25, 27 );
SQL>SELECT * FROM CUSTOMERS WHERE AGE BETWEEN 25 AND 27;
Output:
ID NAME AGE ADDRE SALAR
SS Y
1 Akshay 25 Delhi 30000
3 Kushagra 26 Kolkata 30000
2 rows selected.
CASE STUDY:
1. CREATION OF BANK DATABASE SYSTEM using mysql
The bank database schema has a combination of multiple tables,
where we will creating database
schema tables which is more helpful to design a bank database.
Bank database is a collection of multiple tables like customer,
branch, account, trandetails, loan.
Now we will be writing tables to create bank database schema:
First, create the database "bank"
mysql> CREATE DATABASE bank;
Then use the database bank to creating tables inside it.
mysql> USE bank;
All the tables are created now we will be inserting database records into these tables:
Insert records into the customer table:
Record :
INSERT INTO customer VALUES('C00001','Ramesh','Chandra','Sharma','Delhi','9543198345','Service','1976-12-06');
INSERT INTO customer VALUES('C00002','Avinash','Sunder','Minha','Delhi','9876532109','Service','1974-
10-16');
INSERT INTO customer
VALUES('C00003','Rahul',null,'Rastogi','Delhi','9765178901','Student
','1981-09-26');INSERT INTO customer
VALUES('C00004','Parul',null,'Gandhi','Delhi','9876532109','Housewif
e','1976-11-03');INSERT INTO customer
VALUES('C00005','Naveen','Chandra','Aedekar','Mumbai','8976523190','
Service','1976-09-19');INSERT INTO customer
VALUES('C00006','Chitresh',null,'Barwe','Mumbai','7651298321','Stude
nt','1992-11-06');INSERT INTO customer
VALUES('C00007','Amit','Kumar','Borkar','Mumbai','9875189761','Stude
nt','1981-09-06');INSERT INTO customer
VALUES('C00008','Nisha',null,'Damle','Mumbai','7954198761','Service'
,'1975-12-03');INSERT INTO customer
VALUES('C00009','Abhishek',null,'Dutta','Kolkata','9856198761','Serv
ice','1973-05-22');INSERT INTO customer
VALUES('C00010','Shankar',null,'Nair','Chennai','8765489076','Servic
e','1976-07-12');
Lab Activity 3: Open school database, then select student table and
use following SQL statements.
TYPE THE STATEMENT, PRESS ENTER AND NOTE THE OUTPUT
1. To display the STUDENT table structure.
DESCRIBE Student;
2. To add a column (FIELD) in the STUDENT table, for example
TeacherID as VARCHAR(20);
ALTER TABLE Student ADD TeacherID VARCHAR(20);
3. Type the statement
DESC Student;
Press enter key, now note the difference in table structure.
4. Type the statement and press enter key, note the new field that
you have added as TeacherID
SELECT * FROM student;
3.To modify the TeacherID data type form character to integer
ALTER TABLE Student MODIFY TeacherID INTEGER ;
DESC Student;
SELECT * FROM student;
Lab Activity 4
1. To Drop (Delete) a field form a table. For e.g you want to delete TeacherID field.
ALTER TABLE Student DROP TeacherID;
2. To subtract 5 form all students percentage and display name and percentage.
SELECT name, percentage - 5 FROM Student;
3. Using column alise for example we want to display StdName as Student Name and DOB
as Date of Birth
then the statement will be.
SELECT StdName AS "Student Name",
DOB As “Date of Birth” FROM Student;
4. Display the name of all students whose stream is not Science
SELECT StdName FROM student
WHERE Stream <> ‘Science’;
5. Display all name and percentage where percentage is between 60 and 80
SELECT StdName, percentage FROM student WHERE percentage >=60 AND
percentage<=80 ;
Lab Activity 5:
1. To change a student name from SWATI MISHRA to SWATI VERMA whose
StdID is 1014 and also change
percentage 86.
UPDATE Student SET StdName = ‘SWATI VERMA’, percentage = 86
WHERE StdId = 1014;
2. To delete the records form student table where StdId is 1016.
DELETE FROM Student WHERE StdID = 1016;
3. Type the following SQL statement and note the output.
SELECT * FROM Student WHERE StdName LIKE 'G_' ;
SELECT * FROM Student WHERE StdName='G';
SELECT * FROM Student WHERE StdName LIKE 'G%' ;
SELECT * WHERE Student WHERE StdName='%G%' ;
4. Display all the streams in student table.
SELECT DISTINCT Stream FROM Student;
5. Note the output of the following statement.
SELECT StdName, Sex, Stream FROM Student WHERE percentage BETWEEN 70
AND 80;
67