0% found this document useful (0 votes)
25 views

Structured Query Language - Part II

Uploaded by

shazakhalid14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Structured Query Language - Part II

Uploaded by

shazakhalid14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Science Notes

Structured Query Language – Part II

DML Commands
INSERT INTO statement:

INSERT INTO statement is used to insert new records in a table.

Syntax:

INSERT INTO <tablename> VALUES(<value 1>, <value 2>,....);

E.g.If the table Student is as follows:

Relation: Student

AdNo Name Sex Class Section Average


101 Anu F 12 A 85
105 Balu M 12 D 65
203 Leena F 11 B 95
205 Madhu F 10 B 75
305 Surpreeth M 9 C 70
483 Usha F 6 A 60

To add a new row, we give the following statement:

INSERT INTO Student VALUES(110,’Brinda’,’F’,11,’D’,74);

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.

INSERT INTO Student VALUES(211,’Chinmay’,’M’,10,NULL,82);

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.

INSERT INTO Student(Admno,Name,Class) VALUES(410,’Madhu’,10);

DELETE statement:

DELETE statement is used to delete/remove one or more records from a table.

Syntax:

DELETE FROM <tablename> [WHERE <condition>];

E.g.

DELETE FEOM Student WHERE Adno=305;


If no WHERE clause is provided, all the rows are erased from the table, but the empty table will remain in the database.

E.g.

DELETE FROM Student;

UPDATE statement:

The UPDATE statement is used to make modifications in existing data.

Syntax:

UPDATE <table_name> SET <attribute1> = <value1>[, <attribute2> = <value2>, ...] [WHERE <condition>];

UPDATE Student SET Name=’Manu’ WHERE Adno=101;

The SELECT statement:

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:

SELECT attribute1[, attribute2, ... ] FROM <table>name [WHERE <condition>];

E.g.

SELECT Empno, Empname FROM emp;

To select all the data available in a table, we use the following select statement:

SELECT * FROM <tablename>;

E.g.

SELECT * FROM emp;

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.

SELECT empname, mgr AS 'Manager No' FROM emp;

DISTINCT Clause

The SELECT statement when combined with DISTINCT clause, returns records without repetition.

E.g.

SELECT DISTINCT job FROM Emp;


WHERE Clause

The WHERE clause is used to retrieve data that meet some specified conditions.

E.g.

SELECT empname, sal FROM emp WHERE deptno=20;

Relational operators (<=, >, >=, =, !=) can be used to specify such conditions. The logical operators AND, OR, and NOT
are used to combine multiple conditions.

E.g.

SELECT empname, job FROM emp WHERE sal>2000;

SELECT empno FROM emp WHERE deptno!=30;

SELECT empname FROM emp WHERE job=’clerk’ and sal<1000;

SELECT empno FROM emp WHERE deptno=10 or deptno=30;

The BETWEEN operator:

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;

Membership operator IN:

E.g.The IN operator compares a value with a set of values and returns true if the value belongs to that set.

SELECT empname FROM emp WHERE job IN (’Salesman’, ’clerk’,’ Analyst’);

SELECT empname FROM emp WHERE job NOT IN (’clerk’,’ Analyst’);

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.

SELECT empno,empname FROM emp WHERE job='manager' ORDER BY sal ;

SELECT empno,empname FROM emp WHERE job='manager' ORDER BY empno DESC;


Handling NULL Values:

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.

SELECT * FROM emp WHERE deptno IS NULL;

SELECT empno, job FROM emp WHERE comm IS NOT NULL;

Substring pattern matching:

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:

• % (per cent)- used to represent zero, one, or multiple characters

• _ (underscore)- used to represent exactly a single character

E.g.

SELECT empname FROM emp WHERE empname LIKE "M%";

SELECT empname FROM emp WHERE empname LIKE "%S";

SELECT empno, job FROM emp WHERE job LIKE "%MAN%";

SELECT empname FROM emp WHERE empname LIKE "_A%";

SELECT empname FROM emp WHERE empname LIKE "_ _ _ _";

SELECT empname FROM emp WHERE empname LIKE "_ _ _ _ _%";

You might also like