Unit 3 Chapter-2 SQL (Structured Query Language)
Unit 3 Chapter-2 SQL (Structured Query Language)
Chapter - 2
SQL
Structured Query Language
2. No Coding Skills –
For data retrieval, large number of lines of code is not required. The
syntactical rules are not complex in SQL, which makes it a user-friendly language.
3. Standardised Language –
Due to documentation and long establishment over years, it provides a uniform
platform worldwide to all its users.
4. Portable –
It can be used in programs independent of any platform (Operating System, etc).
5. Interactive Language –
Easy to learn and understand, answers to complex queries can be received in
seconds.
• SQL is case insensitive. That means name and NAME are same
for SQL.
• To enter multiline SQL statements, we don’t write ‘;’ after the first
line. We put enter to continue on next line.
The prompt mysql> then changes to ‘->’, indicating that
statement is continued to the next line. After the last line, put ‘;’
and press enter.
Syntax:
CREATE DATABASE databasename;
Syntax:
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
.....
.....
attributenameN datatype constraint);
Student
Marks
Admno (PK) integer (5)
Sname varchar(15) SrNo (PK) int(3)
Gender char(1) Admno (FK) int(5)
Dob date Sub_Code (FK) int(4)
Fname varchar(15) Marks decimal(6,2)
Contactno bigint(10)
Subject
Subject Marks
SrNo Admno Sub_Code Marks
Sub_Code Subname
1 3154 027 75.00
027 History
2 3154 029 82.00
029 Geography
3 3154 065 90.00
030 Economics
4 4156 041 80.00
041 Mathematics
5 4156 042 76.00
042 Physics
6 4156 043 81.00
043 Chemistry
7 4156 065 99.00
055 Accountancy
8 9910 030 70.00
065 Informatics Practices
9 9910 055 55.00
10 9910 065 92.00
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
SQL for DATA DEFINITION
CREATE Table
To create table(s) called Student, Subject and Marks. We type
following command at MySQL prompt.
We can view the structure of an already created table using the DESC or
DESCRIBE statement.
Syntax:
DESCRIBE tablename;
In all such cases, we need to change or alter the structure of the table by
using the alter statement.
Syntax:
ALTER TABLE tablename ADD/Modify/DROP attribute1,
attribute2,.. ;
Syntax:
ALTER TABLE table_name ADD attribute_name DATATYPE;
e.g.
ALTER TABLE STUDENT ADD AADHARNO VARCHAR(12);
Syntax:
ALTER TABLE table_name MODIFY attribute DATATYPE;
e.g.
ALTER TABLE STUDENT MODIFY SNAME VARCHAR(20);
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA DEFINITION
ALTER Table
Remove an attribute
Syntax:
ALTER TABLE table_name DROP attribute;
e.g.
ALTER TABLE STUDENT DROP AADHARNO;
Syntax:
INSERT INTO tablename
VALUES(value 1, value 2,....);
Syntax:
INSERT INTO tablename (column1, column2, ...) VALUES
(value1, value2, ...);
All the remaining columns i.e. Gender, DOB and FNAME will store
NULL or DEFAULT values.
Syntax:
UPDATE table_name
SET attribute1 = value1, attribute2 = value2, ...
WHERE condition;
Syntax:
DELETE FROM table_name
WHERE condition;
Syntax:
SELECT attribute1, attribute2, ...
FROM table_name
WHERE condition
Here, attribute1, attribute2, ... are the column names of the table
table_name from which we want to retrieve data.
The WHERE clause is optional and is used to retrieve data that meet
specified condition(s).
Designed by: Umesh Pun (PGT IP)
Courtesy: NCERT
APS Yol Cantt
SQL for DATA QUERY
• Display all the employees who are earning more than 5000 and work in
department with DeptId D04.
• Displays name and department number of all those employees who are
earning salary between 20000 and 50000 (both values inclusive).
Note: The BETWEEN operator defines the range of values in which the column
value must fall into, to make the condition true.
OR
Note: IN operator compares a value with a set of values and returns true if
the value belongs to that set.
• Displays details of all those employees who have not been given a bonus.
• Displays details of all those employees who have been given a bonus.
SQL provides LIKE operator that can be used with WHERE clause to search
for a specified pattern in a column.
The LIKE operator makes use of the following two wild card characters:
• Display details of all those employees whose name starts with 'K’.
SELECT * FROM EMPLOYEE WHERE Ename LIKE 'K%';
• Display details of all those employees whose name ends with ‘a’.
SELECT * FROM EMPLOYEE WHERE Ename LIKE ‘%a';