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

Types of SQL Commands

The document provides an overview of query languages, specifically focusing on SQL, which is a non-procedural language used for data manipulation in relational databases. It details various SQL commands, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), Data Control Language (DCL), and Transaction Control Language (TCL), along with their functionalities. Additionally, it covers aggregate functions, subqueries, complex queries, and different types of SQL joins, explaining how they can be used to retrieve and manipulate data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Types of SQL Commands

The document provides an overview of query languages, specifically focusing on SQL, which is a non-procedural language used for data manipulation in relational databases. It details various SQL commands, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), Data Control Language (DCL), and Transaction Control Language (TCL), along with their functionalities. Additionally, it covers aggregate functions, subqueries, complex queries, and different types of SQL joins, explaining how they can be used to retrieve and manipulate data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

QUERY LANGUAGE:-

 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

Some of the important features of SQL are:


 It is a non procedural language.
 It is an English-like language.
 It can process a single record as well as sets of records at a time.
 It is different from a third generation language (C& COBOL). All SQL statements define
what is to be done rather than how it is to be done.
 SQL is a data sub-language consisting of three built-in languages
 Data definition language (DDL), Data manipulation language (DML) and Data Control
language (DCL).
 It insulates the user from the underlying structure and algorithm.
 SQL has facilities for defining database views, security, integrity constraints, transaction
controls, etc

Types of SQL Commands


SQL commands are of varied types to suit different purposes.
The primary types are as follows:
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Query Language (DQL)
Data Control Language (DCL)
Transactional control language (TCL)

Data Definition Language (DDL)


Data definition language (DDL)
 The Data definition language (DDL) defines a set of commands used in the creation and
modification of schema objects such as tables, indexes, views etc.
 These commands provide the ability to create, alter and drop these objects.
 These commands are related to the management and administrations of the databases.
 Before and after each DDL statement, the current transactions are implicitly committed, that is
changes made by these commands are permanently stored in the databases

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

Data Manipulation Language (DML)


Data Manipulation Language or DML is that part of SQL which is used to manipulate data
within objects of a relational database. There are three basic DML commands:
INSERT
UPDATE
DELETE

Data Query Language (DQL)


Though comprising only one command, Data Query Language (DQL) is the most concentrated
focus of SQL for modern relational database users. The base command is as follows:
SELECT
This command, accompanied by many options and clauses, is used to compose queries against a
relational database. Queries, from simple to complex, from vague to specific, can be easily
created. A query is an inquiry to the database for information. A query is usually issued to the
database through an application interface or via a command line prompt. It is to be noted that,
according to some authors, the SELECT command may be treated as DML as a query is nothing
but a part of DML that is used for retrieval of data from a database.

Data Control Language (DCL)


Data control commands in SQL allow us to control access to data within the database. These
DCL commands are normally used to create objects related to user access and also control the
distribution of privileges among users. Some data control commands are as follows:
GRANT
REVOKE

Transactional Control Languages (TCL)


In addition to the previously introduced categories of commands, the following commands allow
the user to manage database transactions:
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
Aggregate Functions

Different aggregate operators that SQL support are :

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.

Solution: SELECT SUM(E.esal) AS sum_salary, MAX(E.esal) AS Max_salary, MIN(E.esal) AS


Min_salary, AVG([DISTINCT] E.esal) AS Average_salary

FROM Employee E.

Query (b): List the number of employee in the company

Solution: SELECT COUNT (*) FROM Employee E.

Query (c): List the number of employees who are working on project number 44

Solution: SELECT COUNT(*)

FROM Employee E, Department D

WHERE E.DNo = D.DNo AND D.PNo = 44.

Query (d): Find the name and age of the eldest employee

Solution: SELECT E.ename, E.age

FROM Employee E

E.age = (SELECT MAX(E2.age) FROM employees E2)


Sub queries

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:

1. They can take the place of a constant.


2. They can take the place of a constant yet vary based on the row being processed.
3. They can return a list of values for use in a comparison.

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.

Example: SELECT AVG(salary)FROM employee WHERE title = „Programmer‟; This


statement will return the average salary for all employees whose title is equal to „Programmer‟

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.

HAVING clause syntax:

SELECT column1, SUM(column2) FROM “list-of-tables”

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:

SELECT dept, avg(salary)

FROM employee

GROUP BY dept;

But, let‟s say that you want to ONLY calculate & display the average if their salary is over
20000:

SELECT dept, avg(salary)

FROM employee

GROUP BY dept

HAVING avg(salary) > 20000;


Complex Queries

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.

You can create a complex query by using the following:

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 with the SELECT Statement

Sub queries are most frequently used with the SELECT statement. The basic syntax is as follows
SELECT column name [, column name]

FROM table1 [, table2]

WHERE column name OPERATOR

(SELECT column name [, column name]

FROM table1 [, table2]

[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.

SQL specifies four types of JOINs:

 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

SELECT table1.column1, table1.column2, table2.column1,

FROM table1

INNER JOIN table2

ON table1.matching_column = table2.matching_column;
Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT

FROM EMPLOYEE

INNER JOIN PROJECT

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

SELECT table1.column1, table1.column2, table2.column1,

FROM

table1

LEFT JOIN table2

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

SELECT table1.column1, table1.column2, table2.column1,

FROM table1 RIGHT JOIN table2

ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT

FROM EMPLOYEE

RIGHT JOIN PROJECT

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.

Syntax SELECT table1.column1, table1.column2, table2.column1,

FROM table1

FULL JOIN table2

ON table1.matching_column = table2.matching_column;

Query

EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT

FULL JOIN PROJECT

ON PROJECT.EMP_ID =EMPLOYEE.EMP_ID

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy