SQL Commands:
SQL commands are instructions, coded into SQL statements, which are used to communicate with
the database to perform specific tasks, work, functions and queries with data.
SQL commands can be used not only for searching the database but also to perform various other
functions like, for example, you can create tables, add data to tables, or modify data, drop the
table, set permissions for users. SQL commands are grouped into four major categories depending
on their functionality:
Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are CREATE,
ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving,
modifying,
and
deleting
data.
These Data Manipulation Language commands are:SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing security to
database objects. These commands are GRANT and REVOKE.
SQL SELECT Statement
The most commonly used SQL command is SELECT statement. SQL SELECT statement is used to
query or retrieve data from a table in the database. A query may retrieve information from
specified columns or from all of the columns in the table. To create a simple SQL SELECT
Statement, you must specify the column(s) name and the table name. The whole query is called
SQL SELECT Statement.
Syntax of SQL SELECT Statement:
SELECT column_list FROM table-name
[WHERE
[GROUP
Clause]
BY
[HAVING
[ORDER BY clause];
table-name is the name of the table from which the information is retrieved.
column_list includes one or more columns from which data is retrieved.
The code within the brackets is optional.
SQL Alias
clause]
clause]
SQL Aliases are defined for columns and tables. Basically aliases is created to make the column
selected more readable.
For Example: To select the first name of all the students, the query would be like:
Aliases for columns:
SELECT
first_name
AS
Name
FROM
student_details;
or
SELECT first_name Name FROM student_details;
In the above query, the column first_name is given a alias as 'name'. So when the result is
displayed the column name appears as 'Name' instead of 'first_name'.
Output:
Name
------------Rahul
Sharma
Anjali
Bhagwat
Stephen
Fleming
Shekar
Gowda
Priya Chandra
SQL WHERE Clause
The WHERE Clause is used when you want to retrieve specific information from a table excluding
other irrelevant data. For example, when you want to see the information about students in class
10th only then you do need the information about the students in other class. Retrieving
information about all the students would increase the processing time for the query.
So SQL offers a feature called WHERE clause, which we can use to restrict the data that is
retrieved. The condition you provide in the WHERE clause filters the rows retrieved from the table
and gives you only those rows which you expected to see. WHERE clause can be used along with
SELECT, DELETE, UPDATE statements.
Syntax of SQL WHERE Clause:
WHERE
Syntax
{column
for
or
WHERE
expression}
clause
comparison-operator
with
Select
statement
value
is:
SELECT column_list FROM table-name
WHERE condition;
column or expression - Is the column of a table or a expression
comparison-operator - operators like = < > etc.
value - Any user value or a column name for comparison
SQL Operators
There are two type of Operators, namely Comparison Operators and Logical Operators. These
operators are used mainly in the WHERE clause, HAVING clause to filter the data to be selected.
Comparison Operators:
Comparison operators are used to compare the column data with specific values in a condition.
Comparison Operators are also used along with the SELECT statement to filter data based on
specific conditions.
SQL Logical Operators
There are three Logical Operators namely, AND, OR, and NOT. These operators compare two
conditions at a time to determine whether a row can be selected for the output. When retrieving
data using a SELECT statement, you can use logical operators in the WHERE clause, which allows
you to combine more than one condition.
SQL Comparison Keywords
There are other comparison keywords available in sql which are used to enhance the search
capabilities of a sql query. They are "IN", "BETWEEN...AND", "IS NULL", "LIKE".
Comparision
Operators
Description
column value is similar to specified
character(s).
column value is equal to any one of
IN
a specified set of values.
column value is between two
BETWEEN...AND values, including the end values
specified in the range.
IS NULL
column value does not exist.
LIKE
SQL LIKE Operator
The LIKE operator is used to list all rows in a table whose column values match a specified pattern.
It is useful when you want to search rows to match a specific pattern, or when you do not know the
entire value. For this purpose we use a wildcard character '%'.
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.
Syntax for using SQL ORDER BY clause to sort data is:
SELECT
column-list
FROM
table_name
[WHERE
condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
database table "employee";
id
100
101
102
103
104
name
dept
age
Ramesh Electrical 24
Hrithik Electronics 28
Harsha Aeronautics 28
Soumya Electronics 22
Priya InfoTech 25
salary
location
25000 Bangalore
35000 Bangalore
35000 Mysore
20000 Bangalore
30000 Mangalore
For Example: If you want to sort the employee table by salary of the employee, the sql query
would be.
SELECT name, salary FROM employee ORDER BY salary;
The output would be like
name
---------Soumya
Ramesh
Priya
Hrithik
Harsha
salary
---------20000
25000
30000
35000
35000
The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.