SQL Commands
SQL Commands
SQL Commands
COMMANDS
WHAT IS SQL?
SQL is Structured Query Language, which is a computer language
immediate results.
SQL is the standard language for Relation Database System. All
Syntax:
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
…………………
columnN datatype
);
Example:
SQL>
create table Student
(
id number(5),
name varchar(20),
age number(2)
);
The above command will create a new table Student in database
system with 3 columns id, name and age.
2. ALTER:
Alter command is used for alteration of table structures.
There are various uses of alter command such as,
i. to add a column to existing table.
ii. to change datatype of any column or to modify its size.
Syntax :
drop table table_name;
Example:
drop table Student;
DATA MANIPULATION LANGUAGE
DML commands are used to manipulate the data in the
database.
It provides the data manipulation techniques like selection,
insertion, deletion, updation, modification, replacement,
sorting and display of data or records.
Few of the basic DML commands are:
1. INSERT:
Insert command is used to add new rows of data to a table.
There are two basic syntaxes for insert command.
Syntax:
update table_name set column_name = value where condition;
Example:
update Student set name=‘Abhi’ where id=102;
3. SELECT:
Select command is used to fetch the data from the database.
To fetch the data of complete table the syntax is:
Select column_name1, column_name2…..column-nameN from
table_name;
Select * from tablename;
Example:
Select * from student;
database.
Syntax:
GRANT privilege_name ON object_name TO user_name;
Ex:
SQL>GRANT SELECT ON employee TO user1;
This command grants a SELECT permission on employee table
to user1.
2. REVOKE:
Used to remove access or privileges from the users on the database.
Syntax:
Ex:
When you revoke SELECT privilege on a table for an user, the user
will not be able to select data from employee data.
TRANSACTION CONTROL
LANGUAGE
It includes commands to control the transactions in a database
system.
It makes changes permanent or cancels the changes without
affecting the data.
The commonly used commands are:
1. COMMIT:
The COMMIT command is the transactional command used to
save changes invoked by a transaction to the database.
Syntax:
COMMIT;
.
Write a query to delete students record whose result is FAIL and
COMMIT changes.
Syntax:
ROLLBACK;
Ex: Consider the STUDENT table
Write a query to delete students record whose result is FAIL and
then ROLLBACK the changes.
of characters in a column.
Example: name char(15)
3. VARCHAR/VARCHAR2:
It is used to store variable length alphanumeric data.
of characters in a column.
Example: address varchar2(50)
4. DATE:
It is used to store date in columns.
SQL supports the various date formats other than the standard
DD-MON-YY.
DATE data type stores a date like march 26, 2018
SQL supports the various time formats other than the standard
hh-mm-ss.
It stores a time of day like 12.30 P.M
OPERATORS IN SQL
An operator is a reserved word or a character used in a SQL
statement's to perform operation, such as comparisons and
arithmetic operations.
Different types of operators in SQL are:
1. Arithmetic operators
2. Relational operators
3. Logical operators
ARITHMETIC OPERATORS
Assume variable a holds 10 and variable b holds 20, then
RELATIONAL OPERATORS
Assume variable a holds 10 and variable b holds 20, then
LOGICAL OPERATORS
1. ALL:
The ALL operator in SQL returns true when value matches with all
in column.
Syntax:
SELECT column1, column2…columnN FROM tablename1
WHERE column1 = ALL(SELECT column1 FROM tablename2);
Ex:
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
Example:
SELECT * from employees WHERE salary>15000 AND
age>30;
3. OR:
When we have multiple conditions in SQL statement, we use
OR operator to combine the conditions.
Query will be executed if any one of the condition is true.
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE [condition1] OR [condition2]...OR [conditionN];
Example:
SELECT * from employees WHERE salary>15000 OR age>30;
4. BETWEEN:
Used to search for values that are within a set of values, given
the minimum value and maximum value.
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE column_name BETWEEN value_1 AND value_2;
Example:
SELECT * from employees WHERE salary BETWEEN
5000 AND 10000;
5. NOT:
Reverses the meaning of logical operator with which it is used.
Ex: NOT EXISTS, NOT BETWEEN, NOT IN etc.
Called as negate operator.
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE column_name NOT BETWEEN value_1 AND value_2;
Example:
SELECT * from employees WHERE salary NOT BETWEEN
5000 AND 10000;
6. LIKE:
Used to compare a value to similar values using wildcard
operators.
Ex for wildcard operators are % and _
Examples:
Select name from Student where name=‘A%’;
Select * from employees where dept=‘S_ _ _ S’;
Select * from employees where salary=‘2_ _ _0’
7. ANY:
The ANY operator in SQL returns true when value matches
value in column.
Syntax:
SELECT column1, column2 from tablename1 WHERE
column1 = ANY(SELECT column1 from tablename2);
Ex:
Syntax:
SELECT column1, column2 FROM tablename WHERE column
1 IN ('value1','value2','value3');
Ex:
The following SQL query will return all employees where
location in 'chennai', 'guntur'.
Output:
2. AND:
When we have multiple conditions in SQL statement, we use
AND operator to combine the conditions.
Query will be executed only if all the conditions is true.
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE [condition1] AND [condition2]...AND [conditionN];
Example:
SELECT * from employees WHERE salary>15000 AND
age>30;
3. OR:
When we have multiple conditions in SQL statement, we use
OR operator to combine the conditions.
Query will be executed if any one of the condition is true.
Syntax:
SELECT column1, column2,…columnN
FROM Table_name
WHERE [condition1] OR [condition2]...OR [conditionN];
Example:
SELECT * from employees WHERE salary>15000 OR age>30;
SQL BUILT-IN FUNCTIONS
Used for performing processing on string or numeric data.
Basically there are two types of functions:
1. Single row functions: These functions are applicable to
a particular row of a table. Types of single row functions are
Numeric functions, Text functions, Date & Time functions
and Conversion functions.
2. Group functions: These functions operate on group of
rows and return one value for the entire group. Ex: COUNT,
MAX, MIN, AVG and SUM.
ARITHMETIC FUNCTIONS
Consider the following STUDENT TABLE:
REG NO NAME COMB TOTAL RESULT
121 Anusha PCMC 363 Pass
122 Ashok PCMC 179 Fail
123 Kalashre PCMB 523 Pass
e
1. AVG( ): It calculates the average of specified column.
Syntax:
SELECT AVG(column_name) FROM table_name;
Ex: SELECT AVG(total) FROM student;
Output: AVG
355
2. COUNT( ): It returns the number of rows that matches a
specified criteria.
Syntax:
SELECT COUNT(column_name) FROM table_name;
Ex: SELECT COUNT(*) FROM student;
Output: COUNT(*)
3
3 Ashok 23 3000
1 Ramesh 35 1500
2 Kavya 25 1560
SQL CONSTRAINTS
Constraints are the rules enforced on data columns on table.
These are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the
database.
Following are commonly used constraints available in SQL:
NULL value
NOT NULL Constraint
DEFAULT Constraint
UNIQUE Constraint
PRIMARY Key
FOREIGN Key
CHECK Constraint
NULL Value
A NULL value in a table is a value in a field that appears to be blank,
which means a field with a NULL value is a field with no value.
It is important to understand that a NULL value is different than a zero
value.
A field with a NULL is one that has been left blank during insertion of
rows.
Ex:
SQL> insert into student values('18C1302',‘aayush');
SQL> insert into student values('18C1303','');
SQL> select *from student;
RNO NAME
---------- ---------------
18C1302 aayush
NOT NULL Constraint
It ensures that a column cannot have NULL value.
When a column is defined as NOT NULL then the column
becomes a mandatory column.
Ex:
CREATE TABLE product
(
pid char (4) NOT NULL,
description varchar2 (25) NOT NULL,
companyid char (10),
dom date,
type char (10),
price number (10,2)
);
DEFAULT Constraint
It provides a default value for a column when no data is specified
during insertion of values.
Ex:
CREATE TABLE product
(
pid char (4),
description varchar2 (25),
companyid char (10),
dom date,
type char (10),
price number (10, 2) default 1000.00
);
UNIQUE Constraint
This constraint ensures that no rows have the same value in the
specified column.
2. Example:- 2. Example:-
SELECT ID, Name FROM Select ID, Name, Percent,
student ORDER BY result from STUDENT
Name; GROUP BY COMB
Difference between data definition language and data manipulation language.
DDL DML