SQL Notes
SQL Notes
4
▪A field is a column in a table that is designed to maintain
specific related information about every record in the table.
6
Components of SQL
▪SQL commands are divided into four categories:
1. To create a database, type the following command.
CREATE DATABASE database_name;
For example to create a database to store the tables,
CREATE DATABASE stud;
2. To work with the database, type the following command.
USE DATABASE;
For example to use the stud database created,
USE stud;
▪ The Data Definition Language (DDL) consist of SQL statements
used to define the database structure or schema.
▪ It simply deals with descriptions of the database schema and is
used to create and modify the structure of database objects in
databases.
▪ The DDL provides a set of definitions to specify the storage
structure and access methods used by the database system.
▪SQL commands which come under Data Definition Language
are:
Data Definition Language (DDL)
Command Description
CREATE Creates table in the database.
ALTER Alters the structure of the database.
DROP Deletes table from database.
By Data Manipulation we mean,
▪Insertion of new information into the database
▪Retrieval of information stored in a database.
▪Deletion of information from the database.
▪Modification of data stored in the database.
▪SQL commands which come under Data Manipulation
Language are :
Data Manipulation Language (DML)
Command Description
INSERT Inserts data into a table
UPDATE Updates the existing data within a table.
Deletes all records from a table, but not the space
DELETE
occupied by them.
Data Type Description
Fixed width string value. Values of this type is
char
enclosed in single quotes.
(Character)
For ex. Anu’s will be written as ‘Anu’‘s’.
Variable width character string. This is similar to
varchar char except the size of the
data entry vary considerably.
It is same as decimal except that the maximum
integer number of digits may not
exceed the precision argument.
Data Type Description
dec It represents a fractional number such as 15.12,
(Decimal) 0.123 etc.
;
▪The DROP TABLE command is used to remove a table from
the database.
▪Once a table is dropped we cannot get it back, so be careful
while using DROP TABLE command.
▪Once all the rows are deleted, the table can be deleted by
DROP TABLE command in the following way:
The above command will change the age to 20 for those students
whose place is “Bangalore”.
▪The table will be updated as follows:
values occur.
▪ The BETWEEN keyword defines a range of values the record must fall
into to make the condition true.
▪ The range may include an upper value and a lower value between
which thecriteria must fall into.
SELECT Admno, Name, Age, Gender FROM Student WHERE
Age BETWEEN 18 AND 19;
Admno Name Gender Age
101 Adarsh M 18
103 Ayush M 18
104 Abinandh M 18
105 Revathi F 19
106 Devika F 19
▪The NOT BETWEEN is reverse of the BETWEEN operator
where the records not satisfying the condition are displayed.
a)%
b)_
• Search for employee whose name begins from ‘s’
like ‘s%’;
like ‘%-02-%’
‘s_ _ _ _ ’;
▪The ORDER BY clause in SQL is used to sort the data in either
ascending or descending based on one or more columns.
1. By default ORDER BY sorts the data in ascending order.
2. We can use the keyword DESC to sort the data in descending
order and the keyword ASC to sort in ascending order.
The ORDER BY clause is used as:
SELECT <column-name>[,<column-name>,….] FROM <table-
name> ORDER BY <column1>,<column2>,… ASC | DESC ;
▪To display the students in alphabetical order of their names:
SELECT * FROM Student ORDER BY Name ;
Admno Name Gender Age Place
104 Abinandh M 18 Chennai
101 Adarsh M 18 Delhi
102 Akshith M 17 Bangalore
100 Ashish M 17 Chennai
103 Ayush M 18 Delhi
106 Devika F 19 Bangalore
107 Hema F 17 Chennai
105 Revathi F 19 Chennai
SELECT * FROM Student WHERE Age>=18 ORDER BY Name;
Admno Name Gender Age Place
104 Abinandh M 18 Chennai
101 Adarsh M 18 Delhi
103 Ayush M 18 Delhi
106 Devika F 19 Bangalore
105 Revathi F 19 Chennai
AGGREGATE functions
1.SUM()
2.AVG()
3.COUNT()
4.MAX()
5.MIN()
6.COUNT(*)
Select SUM(salary) from emp;
Output – 161000
Select SUM(salary) from emp where dept=‘sales’;
Output - 59000
Select AVG(salary) from emp;
Output – 32200
Select AVG(salary) from emp where dept=‘sales’;
Output - 29500
COUNT(name)
Output – 5
COUNT(salary)
Output - 1
COUNT(DISTINCT dept)
Output – 3