You are on page 1of 21

1) DDL is used to create the database schema. DML is used to populate and manipulate database.

2) Full form- Data Definition Language. Data Manipulation Language

3) DDL is not classified further. DML is further classified as Procedural and Non-Procedural DMLs.

4) Commands- DDL- CREATE, ALTER, DROP, TRUNCATE AND COMMENT and RENAME, etc.

DML- SELECT, INSERT, UPDATE, DELETE, MERGE, CALL, etc.

SQL (Structured Query Language) is a standardized programming language that's used to manage
relational databases and perform various operations on the data in them. Initially created in the 1970s,
SQL is regularly used not only by database administrators, but also by developers writing data integration
scripts and data analysts looking to set up and run analytical queries.

The uses of SQL include modifying database table and index structures; adding, updating and deleting
rows of data; and retrieving subsets of information from within a database for transaction processing
and analytics applications. Queries and other SQL operations take the form of commands written as
statements -- commonly used SQL statements include select, add, insert, update, delete, create, alter
and truncate.

SQL became the de facto standard programming language for relational databases after they emerged in
the late 1970s and early 1980s. Also known as SQL databases, relational systems comprise a set of tables
containing data in rows and columns. Each column in a table corresponds to a category of data -- for
example, customer name or address -- while each row contains a data value for the intersecting column.

SQL standard and proprietary extensions


An official SQL standard was adopted by the American National Standards Institute (ANSI) in 1986 and
then by the International Organization for Standardization, known as ISO, in 1987. More than a half-
dozen joint updates to the standard have been released by the two standards development bodies since
then; as of this writing, the most recent version is SQL:2011, approved that year.

Both proprietary and open source relational database management systems built around SQL are
available for use by organizations. They include Microsoft SQL Server, Oracle Database, IBM DB2, SAP
HANA, SAP Adaptive Server, MySQL (now owned by Oracle) and PostgreSQL. However, many of these
database products support SQL with proprietary extensions to the standard language for procedural
programming and other functions. For example, Microsoft offers a set of extensions called Transact-SQL
(T-SQL), while Oracle's extended version of the standard is PL/SQL. As a result, the different variants of
SQL offered by vendors aren't fully compatible with one another.

SQL commands and syntax

SQL commands are divided into several different types, among them data manipulation language (DML)
and data definition language (DDL) statements, transaction controls and security measures. The DML
vocabulary is used to retrieve and manipulate data, while DDL statements are for defining and modifying
database structures. The transaction controls help manage transaction processing, ensuring that
transactions are either completed or rolled back if errors or problems occur. The security statements are
used to control database access as well as to create user roles and permissions.

An Oracle database is a collection of data treated as a unit. The purpose of a database is to store and
retrieve related information. ... Oracle Database is the first database designed for enterprise grid
computing, the most flexible and cost effective way to manage information and applications.

Features:

- Backup and Recovery of Data:-ORACLE provides Flashback technology which leads to efficient recovery
of data.

- Portable:-The ORACLE database has been ported to many platforms, and as a result, the applications on
Oracle are safe when the OS is changed.
- Speed:-Speed of the ORACLE database is good

- Multiple Database Management:-ORACLE manages various databases at the same time. The user can
quickly move at a place where data is actually stored from the node to node in a network

- Efficient Data Integrity:-ORACLE uses the integrity constraints due to which invalid data entry is
restricted to be entered into the database tables.

- Provides Cursor Support:-With the help of cursor, programming becomes easy due to the row-to-row
processing.

Datatypes supported:

1) CHAR (size)

Fixed-length character data of length size bytes.

Fixed for every row in the table (with trailing blanks); maximum size is 2000 bytes per row, default size is
1 byte per row. Consider the character set (one-byte or multibyte) before setting size.

2) VARCHAR2 (size)

Variable-length character data.

Variable for each row, up to 4000 bytes per row. Consider the character set (one-byte or multibyte)
before setting size. A maximum size must be specified.

3) NCHAR(size)
Fixed-length character data of length size characters or bytes, depending on the national character set.

Fixed for every row in the table (with trailing blanks). Column size is the number of characters for a fixed-
width national character set or the number of bytes for a varying-width national character set. Maximum
size is determined by the number of bytes required to store one character, with an upper limit of 2000
bytes per row. Default is 1 character or 1 byte, depending on the character set.

4) NVARCHAR2 (size)

Variable-length character data of length size characters or bytes, depending on national character set. A
maximum size must be specified.

Variable for each row. Column size is the number of characters for a fixed-width national character set or
the number of bytes for a varying-width national character set. Maximum size is determined by the
number of bytes required to store one character, with an upper limit of 4000 bytes per row. Default is 1
character or 1 byte, depending on the character set.

5) CLOB

Single-byte character data.

Up to 2^32 - 1 bytes, or 4 gigabytes.

6) NCLOB

Single-byte or fixed-length multibyte national character set (NCHAR) data.


Up to 2^32 - 1 bytes, or 4 gigabytes.

7) LONG

Variable-length character data.

Variable for each row in the table, up to 2^31 - 1 bytes, or 2 gigabytes, per row. Provided for backward
compatibility.

8) NUMBER (p, s)

Variable-length numeric data. Maximum precision p and/or scale s is 38.

Variable for each row. The maximum space required for a given column is 21 bytes per row.

9) DATE

Fixed-length date and time data, ranging from Jan. 1, 4712 B.C.E. to Dec. 31, 4712 C.E.

Fixed at 7 bytes for each row in the table. Default format is a string (such as DD-MON-YY) specified by
NLS_DATE_FORMAT parameter.

10) BLOB

Unstructured binary data.


Up to 2^32 - 1 bytes, or 4 gigabytes.

11) BFILE

Binary data stored in an external file.

Up to 2^32 - 1 bytes, or 4 gigabytes.

12) RAW (size)

Variable-length raw binary data.

Variable for each row in the table, up to 2000 bytes per row. A maximum size must be specified.
Provided for backward compatibility.

13) LONG RAW

Variable-length raw binary data.

Variable for each row in the table, up to 2^31 - 1 bytes, or 2 gigabytes, per row. Provided for backward
compatibility.

14) ROWID

Binary data representing row addresses.


Fixed at 10 bytes (extended ROWID) or 6 bytes (restricted ROWID) for each row in the table.

15) MLSLABEL

Trusted Oracle datatype.

See the Trusted Oracle documentation.

SQL is one of the most demanding skill in the current world. Every day a huge amount of data is collected
and one have to deal with these databases to make an insightful information. Hence it is important for us
to learn SQL as it is a special-purpose database programming language which help to generate useful
strategies from a database and can easily interact with large and massive database, no matter what is
the size. These features of SQL make SQL a most powerful tool.

Features:

High Performance

SQL provide high performance programming capability for highly transactional, heavy workload and high
usage database system. SQL programming gives various ways to describe the data more analytically.
High Availability

SQL is compatible with databases like MS Access, Microsoft SQL Server, MySQL, Oracle Database, SAP
HANA, SAP Adaptive Server, etc. All of these relational database management systems support SQL and
it is easy to create an application extension for procedural programming and various other functions
which is additional features thus converting SQL into a powerful tool.

Scalability and Flexibility

SQL provide Scalability and Flexibility. It is very easy to create new tables and previously created or not
used tables can be dropped or deleted in a database.

Robust Transactional Support

With SQL programming can handle large records and manage numerous transactions.

High Security

It is very easy to provide permissions on tables, procedures, and views hence SQL give security to your
data.

Comprehensive Application Development

SQL is used by many programmers to program apps to access a database. No matter what is the size of
organization, SQL works for every small or large organization.

Management Ease

SQL is used in almost every relational database management system. “Select“, “Create”, “Insert”, “Drop”,
“Update”, and “Delete” are the standard and common SQL commands that helps us to manage large
amount of data from a database very quickly and efficiently.

Open Source

SQL is an open-source programming language for building relational database management system
The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

SELECT Syntax

SELECT column1, column2, ...

FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

The following code is an example, which would fetch the ID, Name and Salary fields of the customers
available in CUSTOMERS table.

SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;

The SELECT statement has many optional clauses:

1) WHERE specifies which rows to retrieve.

2) GROUP BY groups rows sharing a property so that an aggregate function can be applied to each group.

3) HAVING selects among the groups defined by the GROUP BY clause.

4) ORDER BY specifies an order in which to return the rows.


5) AS provides an alias which can be used to temporarily rename tables or columns.

Here is the format of the SELECT statement:

SELECT [ALL | DISTINCT] column1[,column2] FROM table1[,table2] [WHERE "conditions"] [GROUP BY


"column-list"] [HAVING "conditions] [ORDER BY "column-list" [ASC | DESC] ]

An aggregate function performs a calculation one or more values and returns a single value. The
aggregate function is often used with the GROUP BY clause and HAVING clause of the SELECT statement.
Except for COUNT, aggregate functions ignore null values. Aggregate functions are often used with the
GROUP BY clause of the SELECT statement.

All aggregate functions are deterministic. In other words, aggregate functions return the same value
each time that they are called, when called with a specific set of input values.

Aggregate functions

AVG The AVG() aggregate function calculates the average of non-NULL values in a set.

CHECKSUM_AGG The CHECKSUM_AGG() function calculates a checksum value based on a group


of rows.

COUNT The COUNT() aggregate function returns the number of rows in a group, including rows with
NULL values.

COUNT_BIG The COUNT_BIG() aggregate function returns the number of rows (with BIGINT data
type) in a group, including rows with NULL values.

MAX The MAX() aggregate function returns the highest value (maximum) in a set of non-NULL values.
MIN The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL values.

STDEV The STDEV() function returns the statistical standard deviation of all values provided in the

expression based on a sample of the data population.

STDEVP The STDEVP() function also returns the standard deviation for all values in the provided

expression, but does so based on the entire data population.

SUM The SUM() aggregate function returns the summation of all non-NULL values a set.

VAR The VAR() function returns the statistical variance of values in an expression based on a sample
of the specified population.

VARP The VARP() function returns the statistical variance of values in an expression but does so based
on the entire data population.

Syntax: aggregate_function_name(DISTINCT | ALL expression)

Example: Let's suppose that we want to get the number of times that the movie with id 2 has been
rented out from a table.

SELECT COUNT(`movie_id`) FROM `movierentals` WHERE `movie_id` = 2;

The GROUP BY clause is a SQL command that is used to group rows that have the same values.

The GROUP BY clause is used in the SELECT statement .Optionally it is used in conjunction with
aggregate functions to produce summary reports from the database.
That's what it does, summarizing data from the database.

The queries that contain the GROUP BY clause are called grouped queries and only return a single row
for every grouped item.

GROUP BY Syntax

SELECT statements... GROUP BY column_name1[,column_name2,...] [HAVING condition];

Grouping using a Single Column

In order to help understand the effect of Group By clause, let's execute a simple query that returns all
the gender entries from the members table.

SELECT `gender` FROM `members` ;

gender

Female

Female

Male

Female

Male

Male

Male

Male

Male

Suppose we want to get the unique values for genders. We can use a following query -
SELECT `gender` FROM `members` GROUP BY `gender`;

Executing the above script in MySQL workbench against the Myflixdb gives us the following results.

gender

Female

Male

Note only two results have been returned. This is because we only have two gender types Male and
Female. The GROUP BY clause grouped all the "Male" members together and returned only a single row
for it. It did the same with the "Female" members.

Grouping using multiple columns

Suppose that we want to get a list of movie category_id and corresponding years in which they were
released.

Let's observe the output of this simple query

SELECT `category_id`,`year_released` FROM `movies` ;

category_id year_released

1 2011

2 2008

NULL 2008
NULL 2010

8 2007

6 2007

6 2007

8 2005

NULL 2012

7 1920

8 NULL

8 1920

The above result has many duplicates.

Let's execute the same query using group by -

SELECT `category_id`,`year_released` FROM `movies` GROUP BY `category_id`,`year_released`;

Executing the above script in MySQL workbench against the myflixdb gives us the following results
shown below.

category_id year_released

NULL 2008

NULL 2010

NULL 2012

1 2011

2 2008

6 2007

7 1920

8 1920
8 2005

8 2007

The GROUP BY clause operates on both the category id and year released to identify unique rows in our
above example.

Restricting query results using the HAVING clause

It's not always that we will want to perform groupings on all the data in a given table. There will be times
when we will want to restrict our results to a certain given criteria. In such cases , we can use the
HAVING clause

Suppose we want to know all the release years for movie category id 8. We would use the following
script to achieve our results.

SELECT * FROM `movies` GROUP BY `category_id`,`year_released` HAVING `category_id` = 8;

Executing the above script in MySQL workbench against the Myflixdb gives us the following results
shown below.

movie_id title director year_released category_id

9 Honey mooners John Schultz 2005 8

5 Daddy's Little Girls NULL 2007 8


Note only movies with category id 8 have been affected by our GROUP BY clause.

Summary

The GROUP BY Clause is used to group rows with same values .

The GROUP BY Clause is used together with the SQL SELECT statement.

The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate
functions, constants and expressions.

The HAVING clause is used to restrict the results returned by the GROUP BY clause.

(a) The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

% - The percent sign represents zero, one, or multiple characters

_ - The underscore represents a single character

Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of
the underscore (_).
The percent sign and the underscore can also be used in combinations!

LIKE Syntax

SELECT column1, column2, ...

FROM table_name

WHERE columnN LIKE pattern;

EXAMPLE

The following SQL statement selects all customers with a CustomerName starting with "a":

SELECT * FROM Customers

WHERE CustomerName LIKE 'a%';

(b) The SQL BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

BETWEEN Example

The following SQL statement selects all products with a price BETWEEN 10 and 20:
Example

SELECT * FROM Products

WHERE Price BETWEEN 10 AND 20;

(c) The SQL IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

IN Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name IN (value1, value2, ...);

IN Operator Examples

The following SQL statement selects all customers that are located in "Germany", "France" or "UK":

Example

SELECT * FROM Customers

WHERE Country IN ('Germany', 'France', 'UK');

(d) OR

The OR command is used with WHERE to include rows where either condition is true.
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR city is
"München":

Syntax: SELECT column1, column2, ...

FROM table_name

WHERE condition1 OR condition2 OR condition3 ...;

Example

SELECT * FROM Customers

WHERE City='Berlin' OR City='München';

(e) NULL

A field with a NULL value is a field with no value.

If a field in a table is optional, it is possible to insert a new record or update a record without adding a
value to this field. Then, the field will be saved with a NULL value.

Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value
is one that has been left blank during record creation!

How to Test for NULL Values?

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.

IS NULL Syntax

SELECT column_names
FROM table_name

WHERE column_name IS NULL;

Example

The IS NULL operator is used to test for empty values (NULL values).

The following SQL lists all customers with a NULL value in the "Address" field:

Example

SELECT CustomerName, ContactName, Address

FROM Customers

WHERE Address IS NULL;

1) WHERE clause is processed right after FROM clause in the logical order of query processing, which
means it is processed before GROUP BY clause while HAVING clause is executed after groups are created.

2) If used in GROUP BY, You can refer any column from a table in WHERE clause but you can only use
columns which are not grouped or aggregated.
3) If you use HAVING clause without group by, it can also refer any column but the index will not be used
as opposed to WHERE clause. For example, the following have the same result set, however "where" will
use the id index and having will do a table scan

select * from table where id = 1

select * from table having id = 1

4) You can use an aggregate function to filter rows with HAVING clause. Because HAVING clause is
processed after the rows have been grouped, you can refer to an aggregate function in the logical
expression. For example, the following query will display only courses which have more than 10 students
:

SELECT Course, COUNT(Course) as NumOfStudent from Training GROUP BY Course HAVING


COUNT(COURSE)> 10

5) Another key difference between WHERE and HAVING clause is that WHERE will use Index and HAVING
will not, for example following two queries will produce an identical result but WHERE will use Index and
HAVING will do a table scan

SELECT * FROM Course WHERE Id = 101;

SELECT * FROM Course HAVING Id = 102;

6) Since WHERE clause is evaluated before groups are formed, it evaluates for per row. On the other
hand, the HAVING clause is evaluated after groups are formed hence it evaluates for per group.

You might also like