Topic 10 - Structure Query Language (SQL)
Topic 10 - Structure Query Language (SQL)
Topic 10 - Structure Query Language (SQL)
Introduction to SQL
Structure Query Language (SQL) is a database query language used for storing and managing data in Relational
DBMS. SQL was the first commercial language introduced for E.F Codd's Relational model of database. Today
almost all RDBMS (MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the standard database query
language. SQL is used to perform all types of data operations in RDBMS.
SQL Command
SQL defines following ways to manipulate data stored in an RDBMS.
Command Description
create to create new table or database
alter for alteration
truncate delete data from table
drop to drop a table
rename to rename a table
Command Description
insert to insert a new row
update to update existing row
delete to delete a row
merge merging two rows or two tables
Data control languages are the commands to grant and take back authority from any database user.
Command Description
grant grant permission of right
revoke take back permission.
Creating a Database
To create a database in RDBMS, create command is used. Following is the syntax,
CREATE DATABASE <DB_NAME>;
Creating a Table
create command can also be used to create tables. Now when we create a table, we have to specify the
details of the columns of the tables too. We can specify the names and datatypes of various columns in
the create command itself.
Following is the syntax,
CREATE TABLE <TABLE_NAME>
(
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);
create table command will tell the database system to create a new table with the given table name and
column information.
Example for creating Table
CREATE TABLE Student(
student_id INT,
name VARCHAR(100),
age INT);
The above command will create a new table with name Student in the current database with 3 columns,
namely student_id, name and age. Where the column student_id will only store integer, name will hold upto
100 characters and age will again store only integer value.
If you are currently not logged into your database in which you want to create the table then you can also add
the database name along with table name, using a dot operator .
For example, if we have a database with name Test and we want to create a table Student in it, then we can
do so using the following query:
Using ALTER command we can add a column to any existing table. Following is the syntax,
ALTER command can add a new column to an existing table with a default value too. The default value is used
when no value is inserted in the column. Following is the syntax,
ALTER command can also be used to modify data type of any existing column. Following is the syntax,
Using ALTER command you can rename an existing column. Following is the syntax,
TRUNCATE command
TRUNCATE command removes all the records from a table. But this command will not destroy the table's
structure. When we use TRUNCATE command on a table its (auto-increment) primary key is also initialized.
Following is its syntax,
TRUNCATE TABLE table_name
Here is an example explaining it,
TRUNCATE TABLE student;
The above query will delete all the records from the table student.
In DML commands, we will study about the DELETE command which is also more or less same as
the TRUNCATE command. We will also learn about the difference between the two in that tutorial.
DROP command
DROP command completely removes a table from the database. This command will also destroy the table
structure and the data stored in it. Following is its syntax,
DROP TABLE table_name
Here is an example explaining it,
DROP TABLE student;
The above query will delete the Student table completely. It can also be used on Databases, to delete the
complete database. For example, to drop a database,
DROP DATABASE Test;
The above query will drop the database with name Test from the system.
RENAME query
RENAME command is used to set a new name for any existing table. Following is the syntax,
RENAME TABLE old_table_name to new_table_name
Talking about the Insert command, whenever we post a Tweet on Twitter, the text is stored in some table, and
as we post a new tweet, a new record gets inserted in that table.
INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
INSERT INTO table_name VALUES(data1, data2, ...)
Consider a table student with the following fields.
s_id name age
UPDATE command
UPDATE command is used to update any record of data in a table. Following is its general syntax,
UPDATE table_name SET column_name = new_value WHERE some_condition;
WHERE is used to add a condition to any SQL query, we will soon study about it in detail.
Let’s take a sample table student,
student_id name age
101 Adam 15
102 Alex
103 chris 14
UPDATE student SET age=18 WHERE student_id=102;
DELETE command
DELETE command is used to delete data from a table.
Following is its general syntax,
DELETE FROM table_name;Let's take a sample table student:
s_id name age
101 Adam 15
102 Alex 18
103 Abhi 17
Delete all Records from a Table
DELETE FROM student;The above command will delete all the records from the table student.
Delete a particular Record from a Table
In our student table if we want to delete a single record, we can use the WHERE clause to provide a condition
in our DELETE statement.
DELETE FROM student WHERE s_id=103;
The above command will delete the record where s_id is 103 from the table student.
When we specify a condition using the WHERE clause then the query executes only for those records for which
the condition specified by the WHERE clause is true.
Syntax for WHERE clause
Here is how you can use the WHERE clause with a DELETE statement, or any other statement,
SELECT s_id,
name,
age,
address
FROM student WHERE name = 'Adam';
Following will be the result of the above query.
Wildcard operators
There are two wildcard operators that are used in LIKE clause.
Percent sign %: represents zero, one or more than one character.
Underscore sign _: represents only a single character.
Using % only
SELECT * FROM Student WHERE s_name LIKE '%x';
Syntax of Order By
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;
Using default Order by
Consider the following Emp table,
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SELECT * FROM Emp ORDER BY salary;
The above query will return the resultant data in ascending order of the salary.
The above query will return the resultant data in descending order of the salary.
eid name age salary
404 Scott 44 10000
401 Anu 22 9000
405 Tiger 35 8000
402 Shane 29 8000
403 Rohan 34 6000
Here we want to find name and age of employees grouped by their salaries or in other words, we will be
grouping employees based on their salaries, hence, as a result, we will get a data set, with unique salaries
listed, alongside the first employee's name and age to have that salary. Hope you are getting the point here!
group by is used to group different row of data together based on any one column.
name age
Rohan 34
Shane 29
Anu 22
Example of Group by in a Statement with WHERE clause
Consider the following Emp table
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 9000
405 Tiger 35 8000
SQL query will be,
name salary
Rohan 6000
Shane 8000
Scott 9000
You must remember that Group By clause will always come at the end of the SQL query, just like the Order
by clause.
SELECT *
FROM sale GROUP BY customer
HAVING sum(previous_balance) > 3000
Result will be,
The main objective of the above SQL query was to find out the name of the customer who has had
a previous_balance more than 3000, based on all the previous sales made to the customer, hence we get the
first row in the table for customer Alex.
DISTINCT keyword
The distinct keyword is used with SELECT statement to retrieve unique values from the table. Distinct removes
all the duplicate records while retrieving records from any table in the database.
Syntax for DISTINCT Keyword
SELECT DISTINCT column-name FROM table-name;
salary
5000
8000
10000
SELECT * FROM Emp WHERE salary < 10000 AND age > 25
The above query will return records where salary is less than 10000 and age greater than 25. Hope you get the
concept here. We have used the AND operator to specify two conditions with WHERE clause.
OR operator
OR operator is also used to combine multiple conditions with WHERE clause. The only difference
between AND and OR is their behavior.
When we use AND to combine two or more than two conditions, records satisfying all the specified conditions
will be there in the result.
But in case of OR operator, atleast one condition from the conditions specified must be satisfied by any record
to be in the resultset.
Consider the following Emp table
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000