Professional Documents
Culture Documents
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.
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.
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 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 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 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
6) NCLOB
7) LONG
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 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
11) BFILE
Variable for each row in the table, up to 2000 bytes per row. A maximum size must be specified.
Provided for backward compatibility.
Variable for each row in the table, up to 2^31 - 1 bytes, or 2 gigabytes, per row. Provided for backward
compatibility.
14) ROWID
15) MLSLABEL
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.
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.
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.
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.
SELECT Syntax
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:
The following code is an example, which would fetch the ID, Name and Salary fields of the customers
available in CUSTOMERS table.
2) GROUP BY groups rows sharing a property so that an aggregate function can be applied to each group.
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.
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
STDEVP The STDEVP() function also returns the standard deviation for all values in the provided
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.
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.
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
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.
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.
Suppose that we want to get a list of movie category_id and corresponding years in which they were
released.
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
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.
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.
Executing the above script in MySQL workbench against the Myflixdb gives us the following results
shown below.
Summary
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:
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
FROM table_name
EXAMPLE
The following SQL statement selects all customers with a CustomerName starting with "a":
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
BETWEEN Example
The following SQL statement selects all products with a price BETWEEN 10 and 20:
Example
IN Syntax
SELECT column_name(s)
FROM table_name
IN Operator Examples
The following SQL statement selects all customers that are located in "Germany", "France" or "UK":
Example
(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":
FROM table_name
Example
(e) NULL
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!
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
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
FROM Customers
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
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
:
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
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.