Types of SQL Commands
Types of SQL Commands
A query language is a language in which a user requests information from the database.
These languages are usually on a level higher than that of a standard programming
language.
Query languages can be categorized as either procedural or non-procedural. In a
procedural language the user instructs the system to perform a sequence of operations on
the database to compute the desired result. In a non-procedural language the user
describes the desired information without giving a specific procedure for obtaining that
information
SQL:
Structured Query Language (SQL) is a standard query language.
It is commonly used with all relational databases for data definition and manipulation.
SQL is called a non-procedural language as it just specifies what is to be dome rather than how it
is to be done.
since SQL is a higher-level query language, it is closer to a language like English.
Therefore, it is very user friendly.
The American National Standard Institute (ANSI) has designed standard versions of SQL
Data Definition Language (DDL) is a part of SQL that allows a database user to create and
restructure database objects, such as the creation or deletion of a table. Some of the most
fundamental DDL commands include the following:
CREATE
ALTER
DROP
Count: COUNT followed by a column name returns the count of tuple in that column. If
DISTINCT keyword is used then it will return only the count of unique tuple in the column.
Otherwise, it will return count of all the tuples (including duplicates) count (*) indicates all the
tuples of the column.
SUM: SUM followed by a column name returns the sum of all the values in that column. If
DISTINCT keyword is used then it will return the sum of all unique values in the columns.
AVG: AVG followed by a column name returns the average value of that column values. If
DISTINCT keyword is used then it will return the average of distinct values only
MAX: MAX followed by a column name returns the maximum value of that column.
MIN: Min followed by column name returns the minimum value of that column.
Queries Based on Aggregate Functions Query (a): Find the sum of salaries of all the employees
and also the minimum, maximum and average salary.
FROM Employee E.
Query (c): List the number of employees who are working on project number 44
Query (d): Find the name and age of the eldest employee
FROM Employee E
The expression following WHERE can be either a simple predicate as explained above or it can
be a query itself! This part of the query following WHERE is called a Sub query.
A sub query, which in turn is a query can have its own sub query and the process of specifying
sub queries can continue ad infinitum! More practically, the process ends once the query has
been fully expressed as a SQL statement.
Sub queries can appear when using the comparison predicate, the IN predicate and when
quantifiers are used
Sub queries are similar to SELECT chaining. While SELECT chaining combines SELECTs on
the same level in a query, however, sub queries allow SELECTs to be embedded inside other
queries.
A Sub query or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
A sub query is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Sub queries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
parentheses.
Sub queries
parentheses.
A sub query can have only one column in the SELECT clause, unless multiple columns
are in the main query for the sub query to compare its selected columns.
An ORDER BY command cannot be used in a sub query, although the main query can
use an ORDER BY. The GROUP BY command can be used to perform the same
function as the ORDER BY in a sub query.
Sub queries that return more than one row can only be used with multiple value operators
such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
They can perform several functions:
Sub queries always appear in the HAVING clause or the WHERE clause of a query. A sub
query may itself contain a WHERE clause and/or a HAVING clause, and, consequently.
The HAVING clause allows you to specify conditions on the rows for each group - in other
words, which rows should be selected will be based on the conditions you specify. The
HAVING clause should follow the GROUP BY clause if you are going to use it.
GROUP BY “column-list”
HAVING “condition”;
HAVING can best be described by example. Let‟s say you have an employee table containing
the employee‟s name, department, salary, and age. If you would like to select the average salary
for each employee in each department, you could enter:
FROM employee
GROUP BY dept;
But, let‟s say that you want to ONLY calculate & display the average if their salary is over
20000:
FROM employee
GROUP BY dept
In addition to the simple queries shown in the previous section, you can create complex queries,
which may contain more than one SELECT statement. At the highest level, a query is a
SELECT statement, which consists of a query expression followed by an optional ORDER BY
clause. At the next lower level, you can combine different query blocks into a single query
expression with the UNION operator. Lower still, inside each query block is an optional search
condition, which can contain predicates that incorporate sub queries. A sub query is always a
single query block (SELECT) that can contain other sub queries but cannot contain a UNION. A
query expression can contain a maximum of 16 query blocks from all sources, including
UNION, sub queries, and the outer query block.
1. UNION operator, which allows you to take the union of all rows returned by several query
blocks in one SELECT statement.
2. Sub queries (also known as nested queries), which allow you to embed a query block within
the search condition of an outer SELECT statement.
3. Special predicates, such as ANY, ALL, SOME, EXISTS, and IN, which allow you to
compare the value of an expression with the value of special structures and sub queries.
Sub queries are most frequently used with the SELECT statement. The basic syntax is as follows
SELECT column name [, column name]
[WHERE])
SQL>SELECT * FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY>4500);
SQL JOIN
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables. A JOIN is a means for combining fields from two tables by using
values common to each.
SQL is relational database query language and as such, one of its most important features is its
ability to retrieve information from several different related tables. In relational database terms,
this process is called a join. The tables to be joined are named in the From clause of the Select
with each table name separated by a comma. The relationships between the tables in a join are
defined by the predicate in the Where clause.
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
INNER JOIN : In SQL, INNER JOIN selects records that have matching values in both
tables as long as the condition is satisfied. It returns the combination of all rows from both
the tables where the condition satisfies
Inner joins return all rows from multiple tables where the join condition is met. There must be a
matching value in a field common to both tables. An Inner Join cannot be nested inside a Left
Join or Right Join, it creates a new result table by combining column values of two tables based
upon the join-predicate. The join condition determines whether both records are matched or not.
If there is no match found, no records is returned.
Syntax
FROM table1
ON table1.matching_column = table2.matching_column;
Query
FROM EMPLOYEE
ON PROJECT.EMP_ID =EMPLOYEE.EMP_ID;
LEFT JOIN: The SQL left join returns all the values from left table and the matching values
from the right table. If there is no matching join value, it will return NULL.
In left outer join: rows satisfying selection criteria from both joined tables are selected as well as
all remaining rows from left joined table are being kept along with Nulls instead of actual right
joined table values. or we can say it returns all the values from the left table, plus matched values
from the right table (or NULL in case of no matching join predicate). If the right table returns
one row and the left table returns more than one matching row for it, the values in the right table
will be repeated for each distinct row on the left table.
Syntax
FROM
table1
ON table1.matching_column = table2.matching_column;
RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the
matched values from the left table. If there is no matching in both tables, it will return NULL.
Syntax
ON table1.matching_column = table2.matching_column;
Query
FROM EMPLOYEE
ON PROJECT.EMP_ID =EMPLOYEE.EMP_ID;
FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables
have all the records from both tables. It puts NULL on the place of matches not found.
FROM table1
ON table1.matching_column = table2.matching_column;
Query
EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
ON PROJECT.EMP_ID =EMPLOYEE.EMP_ID