Basic SQL
Basic SQL
Structure Query Language(SQL) is a programming language used for storing and managing data
in RDBMS. SQL was the first commercial language introduced for E.F Codd's Relational
model. Today almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) uses SQL as the
standard database language. SQL is used to perform all type of data operations in RDBMS.
SQL Command
All DDL commands are auto-committed. That means it saves all the changes permanently in the
database.
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
DML commands are not auto-committed. It means changes are not permanent to database, they
can be rolled back.
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
Command Description
commit to permanently save
rollback to undo change
savepoint to save temporarily
Data control language provides command to grant and take back authority.
Command Description
grant grant permission of right
revoke take back permission.
Command Description
select retrieve records from one or more table
create command
create is a DDL command used to create a table or a database.
Creating a Database
Creating a Table
create command is also used to create a table. We can specify names and datatypes of various
columns along.Following is the Syntax,
create table command will tell the database system to create a new table with given table name
and column information.
The above command will create a new table Student in database system with 3 columns, namely
id, name and age.
alter command
alter command is used for alteration of table structures. There are various uses of alter
command, such as,
The above command will add a new column address to the Student table
Using alter command we can even add multiple columns to an existing table. Following is the
Syntax,
The above command will add three new columns to the Student table
alter command can add a new column to an existing table with default values. Following is the
Syntax,
The above command will add a new column with default value to the Student table
alter command is used to modify data type of an existing column . Following is the Syntax,
The above command will modify address column of the Student table
To Rename a column
Using alter command you can rename an existing column. Following is the Syntax,
To Drop a Column
alter command is also used to drop columns also. Following is the Syntax,
The above command will drop address column from the Student table
The above query will delete all the records of Student table.
truncate command is different from delete command. delete command will delete all the rows
from a table whereas truncate command re-initializes a table(like a newly created table).
For eg. If you have a table with 10 rows and an auto_increment primary key, if you use delete
command to delete all the rows, it will delete all the rows, but will not initialize the primary key,
hence if you will insert any row after using delete command, the auto_increment primary key
will start from 11. But in case of truncate command, primary key is re-initialized.
drop command
drop query completely removes a table from database. This command will also destroy the table
structure. Following is its Syntax,
The above query will delete the Student table completely. It can also be used on Databases. For
Example, to drop a database,
The above query will drop a database named Test from the system.
DML command
Data Manipulation Language (DML) statements are used for managing data in database. DML
commands are not auto-committed. It means changes made by DML command are not
permanent to database, it can be rolled back.
1) INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
Or,
The above command will insert only two column value other column is set to null.
Suppose the age column of student table has default value of 14.
Also, if you run the below query, it will insert default value into the age column, whatever the
default value may be.
2) UPDATE command
Update command is used to update a row of a table. Following is its general syntax,
3) Delete command
Delete command is used to delete data from a table. Delete command can also be used with
condition to delete a particular row. Following is its general syntax,
The above command will delete the record where s_id is 103 from Student table.
Commit command
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
rollback to savepoint-name;
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
savepoint savepoint-name;
ID NAME
1 abhi
2 adam
4 alex
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
7 bravo
rollback to B;
SELECT * from class;
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
rollback to A;
SELECT * from class;
ID NAME
DCL command
Data Control Language(DCL) is used to control privilege in Database. To perform any operation
in the database, such as for creating tables, sequences or views we need privileges. Privileges are
of two types,
System : creating session, table etc are all types of system privilege.
Object : any command or query to work on tables comes under object privilege.
WHERE clause
Where clause is used to specify condition while retriving data from table. Where clause is used
mostly with Select, Update and Delete query. If condititon specified by where clause is true then
only the result from table is returned.
SELECT column-name1,
column-name2,
column-name3,
column-nameN
from table-name WHERE [condition];
Now we will use a SELECT statement to display data of the table, based on a condition, which
we will add to the SELECT query using WHERE clause.
SELECT s_id,
s_name,
age,
address
from Student WHERE s_id=101;
s_id s_Name age address
101 Adam 15 Noida
SELECT Query
Select query is used to retrieve data from a tables. It is the most used SQL query. We can retrieve
complete tables, or partial by mentioning conditions using WHERE clause.
The above query will fetch information of s_id, s_name and age column from Student table
A special character asterisk * is used to address all the data(belonging to all columns) in a query.
SELECT statement uses * character to retrieve all records from a table.
The above query will show all the records of Student table, that means it will show complete
Student table as result.
The above command will display a new column in the result, showing 3000 added into existing
salaries of the employees.
Like clause
Like clause is used as condition in SQL query. Like clause compares data with an expression
using wildcard operators. It is used to find similar data from the table.
Wildcard operators
There are two wildcard operators that are used in like clause.
The above query will return all records where s_name starts with character 'A'.
The above query will return all records from Student table where s_name contain 'd' as second
character.
Example
The above query will return all records from Student table where s_name contain 'x' as last
character.
Order By Clause
Order by clause is used with Select statement for arranging retrieved data in sorted order. The
Order by clause by default sort data in ascending order. To sort data in descending order DESC
keyword is used with Order by clause.
Syntax of Order By
The above query will return result in ascending order of the salary.
The above query will return result in descending order of the salary.
Group By Clause
Group by clause is used to group the results of a SELECT query based on one or more columns.
It is also used with SQL functions to group the result from one or more tables.
Here we want to find name and age of employees grouped by their salaries
name age
Rohan 34
shane 29
anu 22
name salary
Rohan 6000
Shane 8000
Scott 9000
You must remember that Group By clause will always come at the end, just like the Order by
clause.
HAVING Clause
having clause is used with SQL Queries to give more precise condition for a statement. It is used
to mention condition in Group based SQL functions, just like WHERE clause.
Suppose we want to find the customer whose previous_balance sum is more than 3000.
SELECT *
from sale group customer
having sum(previous_balance) > 3000
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 from database.
Example
The above query will return only the unique salary from Emp table
salary
5000
8000
10000
AND operator
Example of AND
OR operator
OR operator is also used to combine multiple conditions with Where clause. The only difference
between AND and OR is their behaviour. When we use AND to combine two or more than two
conditions, records satisfying all the condition will be in the result. But in case of OR, atleast one
condition from the conditions specified must be satisfied by any record to be in the result.
Example of OR
The above query will return records where either salary is greater than 10000 or age greater than
25.