Structured Query Language - Part II
Structured Query Language - Part II
DML Commands
INSERT INTO statement:
Syntax:
Relation: Student
The order of values should match with the order of columns in the table as created.
If we want to avoid entering the value for a particular attribute, you have to specifically include a NULL in its place.
E.g.
If we want to insert values only for some of the attributes in a table, then we shall specify the attribute names in which
the values are to be inserted.
DELETE statement:
Syntax:
E.g.
E.g.
UPDATE statement:
Syntax:
UPDATE <table_name> SET <attribute1> = <value1>[, <attribute2> = <value2>, ...] [WHERE <condition>];
The SQL statement SELECT is used to retrieve data from the tables in a database and the output is also displayed in
tabular form.
Syntax:
E.g.
To select all the data available in a table, we use the following select statement:
E.g.
Renaming of columns
In case we want to rename any column while displaying the output, it can be done by using the alias 'AS'.
E.g.
DISTINCT Clause
The SELECT statement when combined with DISTINCT clause, returns records without repetition.
E.g.
The WHERE clause is used to retrieve data that meet some specified conditions.
E.g.
Relational operators (<=, >, >=, =, !=) can be used to specify such conditions. The logical operators AND, OR, and NOT
are used to combine multiple conditions.
E.g.
The BETWEEN operator defines the range of values in which the column value must fall into, to make the condition true.
E.g.
SELECT empname,sal FROM emp WHERE sal BETWEEN 1000 AND 2000;
E.g.The IN operator compares a value with a set of values and returns true if the value belongs to that set.
ORDER BY Clause:
ORDER BY clause is used to display data in an ordered form with respect to a specified column. By default, ORDER BY
displays records in ascending order of the specified column’s values. To display the records in descending order, the
DESC (means descending) keyword needs to be written with that column.
E.g.
SQL supports a special value called NULL to represent a missing or unknown value. It is important to note that NULL is
different from 0 (zero). Also, any arithmetic operation performed with NULL value gives NULL. For example: 5 + NULL =
NULL because NULL is unknown hence the result is also unknown. In order to check for NULL value in a column, we use
IS NULL operator.
E.g.
When we try to find matching of only a few characters or values in column values, it is called substring pattern matching.
We cannot match such patterns using = operator as we are not looking for an exact match. SQL provides a LIKE operator
that can be used with the WHERE clause to search for a specified pattern in a column.
The LIKE operator makes use of the following two wild card characters:
E.g.