Lesson5-SQL (Structured Query Language)-More Notes
Lesson5-SQL (Structured Query Language)-More Notes
SQL is a database computer language designed for the retrieval and management of data in
relational database. SQL stands for Structured Query Language.
SQL is the standard language for Relation Database System. All relational database
management systems like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL
Server use SQL as standard database language.
Why SQL?
1. Allows users to access data in relational database management systems.
2. Allows users to describe the data.
3. Allows users to define the data in database and manipulate that data.
4. Allows to embed within other languages using SQL modules, libraries & pre-compilers.
5. Allows users to create and drop databases and tables.
6. Allows users to create view, stored procedure, functions in a database.
7. Allows users to set permissions on tables, procedures, and views
SQL Process:
When you are executing an SQL command for any RDBMS, the system determines the best way
to carry out your request and SQL engine figures out how to interpret the task.
There are various components included in the process. These components are Query Dispatcher,
Optimization Engines, Classic Query Engine and SQL Query Engine, etc. Classic query engine
handles all non-SQL queries but SQL query engine won't handle logical files.
SQL Architecture
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP.
RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access.
Table
The data in RDBMS is stored in database objects called tables. The table is a collection of
related data entries and it consists of columns and rows.
Remember, a table is the most common and simplest form of data storage in a relational
database. Following is the example of a CUSTOMERS table:
Field
Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS table
consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information about every record
in the table.
Record or row
A record, also called a row of data, is each individual entry that exists in a table.
Column
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
For example, a column in the CUSTOMERS table is ADDRESS, which represents location
description and would consist of the following:
NULL value
A NULL value in a table is a value in a field that appears to be blank, which means a field with a
NULL value is a field with no value.
It is very important to understand that a NULL value is different than 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.
SQL Constraints
Constraints are the rules enforced on data columns on table. These are used to limit the type of
data that can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints could be column level or table level.
Column level constraints are applied only to one column whereas table level constraints are
applied to the whole table.
Following are commonly used constraints available in SQL:
1. NOT NULL Constraint: Ensures that a column cannot have NULL value.
2. DEFAULT Constraint: Provides a default value for a column when none is specified.
3. UNIQUE Constraint: Ensures that all values in a column are different.
4. PRIMARY Key: Uniquely identified each rows/records in a database table.
5. FOREIGN Key: Uniquely identified a rows/records in any another database table.
6. CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.
7. INDEX: Use to create and retrieve data from the database very quickly.
Data Integrity:
The following categories of the data integrity exist with each RDBMS:
1. Entity Integrity: There are no duplicate rows in a table.
2. Domain Integrity: Enforces valid entries for a given column by restricting the type, the
format, or the range of values.
3. Referential integrity: Rows cannot be deleted, which are used by other records.
4. User-Defined Integrity: Enforces some specific business rules that do not fall into
entity, domain or referential integrity
Database Normalization
Database normalization is the process of efficiently organizing data in a database.
There are two reasons of the normalization process:
1. Eliminating redundant data, for example, storing the same data in more than one
tables.
2. Ensuring data dependencies make sense.
Both of these are worthy goals as they reduce the amount of space a database consumes and
ensure that data is logically stored. Normalization consists of a series of guidelines that help
guide you in creating a good database structure.
Normalization guidelines are divided into normal forms; think of form as the format or the way a
database structure is laid out. The aim of normal forms is to organize the database structure so
that it complies with the rules of first normal form, then second normal form, and finally third
normal form.
It's your choice to take it further and go to fourth normal form, fifth normal form, and so on, but
generally speaking, third normal form is enough.
1. First Normal Form 1NF
2. Second Normal Form 2NF
3. Third Normal Form 3NF
SQL Syntax
SQL is followed by unique set of rules and guidelines called Syntax.
All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE,
DELETE,ALTER, DROP, CREATE, USE, SHOW and all the statements end with a
semicolon ; .
Important point to be noted is that SQL is NOT case sensitive, which means SELECT and
select have same meaning in SQL statements, but MySQL makes difference in table names. So if
you are working with MySQL, then you need to give table names as they exist in the database.
SQL ORDER BY Clause:
SQL - SELECT DATABASE, USE STATEMENT
When you have multiple databases in your SQL Schema, then before starting your operation, you
would need to select a database where all the operations would be performed.
The SQL USE statement is used to select any existing database in SQL schema.
Syntax:
Basic syntax of USE statement is as follows:
Example:
You can check available databases as follows:
Now, if you want to work with AMROOD database, then you can execute the following SQL
command and start working with AMROOD database:
SQL- CREATE DATABASE
The SQL CREATE DATABASE statement is used to create new SQL database.
Syntax:
Basic syntax of CREATE DATABASE statement is as follows:
Example:
If you want to create new database <testDB>, then CREATE DATABASE statement would be
as follows:
Make sure you have admin privilege before creating any database. Once a database is created,
you can check it in the list of databases as follows:
SQL- Drop or Delete Database
The SQL DROP DATABASE statement is used to drop an existing database in SQL schema.
Syntax:
Basic syntax of DROP DATABASE statement is as follows:
NOTE: Be careful before using this operation because by deleting an existing database would
result in loss of complete information stored in the database.
Make sure you have admin privilege before dropping any database. Once a database is dropped,
you can check it in the list of databases as follows:
SQL- CREATE TABLE
Creating a basic table involves naming the table and defining its columns and each column's data
type.
The SQL CREATE TABLE statement is used to create a new table.
Syntax:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE is the keyword telling the database system what you want to do. In this case,
you want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement.
Then in brackets comes the list defining each column in the table and what sort of data type it is.
The syntax becomes clearer with an example below.
Example:
Following is an example, which creates a CUSTOMERS table with ID as primary key and NOT
NULL are the constraints showing that these fields cannot be NULL while creating records in
this table:
You can verify if your table has been created successfully by looking at the message displayed
by the SQL server, otherwise you can use DESC command as follows:
Now, you have CUSTOMERS table available in your database which you can use to store
required information related to customers.
SQL- Drop or Delete Table
The SQL DROP TABLE statement is used to remove a table definition and all data, indexes,
triggers, constraints, and permission specifications for that table.
NOTE: You have to be careful while using this command because once a table is deleted then all
the information available in the table would also be lost forever.
Syntax:
Basic syntax of DROP TABLE statement is as follows:
Example:
Let us first verify CUSTOMERS table and then we would delete it from the database:
This means CUSTOMERS table is available in the database, so let us drop it as follows:
Now, if you would try DESC command, then you would get error as follows:
SQL –Insert Query
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
Syntax:
There are two basic syntaxes of INSERT INTO statement as follows:
Here, column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.
You may not need to specify the columns name in the SQL query if you are adding values for all
the columns of the table. But make sure the order of the values is in the same order as the
columns in the table. The SQL INSERT INTO syntax would be as follows:
Example:
Following statements would create six records in CUSTOMERS table:
You can create a record in CUSTOMERS table using second syntax as follows:
All the above statements would produce the following records in CUSTOMERS table:
Here, column1, column2...are the fields of a table whose values you want to fetch.
If you want to fetch all the fields available in the table, then you can use the following syntax:
Example:
Consider the CUSTOMERS table having the following records:
Following is an example, which would fetch ID, Name and Salary fields of the customers
available in CUSTOMERS table:
If you want to fetch all the fields of CUSTOMERS table, then use the following query:
Syntax:
The basic syntax of SELECT statement with WHERE clause is as follows:
You can specify a condition using comparison or logical operators like >, <, =, LIKE, NOT, etc.
Example:
Consider the CUSTOMERS table having the following records:
Following is an example which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000:
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS
table for a customer with name Hardik.
Here, it is important to note that all the strings should be given inside single quotes ″
whereas numeric values should be given without any quote as in above example:
You can combine N number of conditions using AND operator. For an action to be taken by the
SQL statement, whether it be a transaction or query, all conditions separated by the AND must
be TRUE.
Example:
Consider the CUSTOMERS table having the following records:
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000 AND age is less than 25 years:
The OR Operator:
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
Syntax:
The basic syntax of OR operator with WHERE clause is as follows:
You can combine N number of conditions using OR operator. For an action to be taken by the
SQL statement, whether it be a transaction or query, only any ONE of the conditions separated
by the OR must be TRUE.
Example:
Consider the CUSTOMERS table having the following records:
Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS
table where salary is greater than 2000 OR age is less than 25 years:
Syntax:
The basic syntax of UPDATE query with WHERE clause is as follows:
Example:
Consider the CUSTOMERS table having the following records:
If you want to modify all ADDRESS and SALARY column values in CUSTOMERS table, you
do not need to use WHERE clause and UPDATE query would be as follows:
You can use WHERE clause with DELETE query to delete selected rows, otherwise all the
records would be deleted.
Syntax:
The basic syntax of DELETE query with WHERE clause is as follows:
Example:
Consider the CUSTOMERS table having the following records:
If you want to DELETE all the records from CUSTOMERS table, you do not need to use
WHERE clause and DELETE query would be as follows:
The percent sign represents zero, one, or multiple characters. The underscore represents a single
number or character. The symbols can be used in combinations.
Syntax:
You can combine N number of conditions using AND or OR operators. Here, XXXX could be
any numeric or string value.
Example:
Here are number of examples showing WHERE part having different LIKE clause with '%' and
'_' operators:
Let us take a real example, consider the CUSTOMERS table having the following records:
Following is an example, which would display all the records from CUSTOMERS table where
SALARY starts with 200:
Note: All the databases do not support TOP clause. For example MySQL supports LIMIT clause
to fetch limited number of records and Oracle uses ROWNUM to fetch limited number of
records.
Syntax:
The basic syntax of TOP clause with SELECT statement would be as follows:
Example:
Consider the CUSTOMERS table having the following records:
Following is an example on SQL server, which would fetch top 3 records from CUSTOMERS
table:
This would produce the following result:
Syntax:
You can use more than one column in the ORDER BY clause. Make sure whatever column you
are using to sort, that column should be in column-list.
Example:
Consider the CUSTOMERS table having the following records:
Following is an example, which would sort the result in ascending order by NAME and
SALARY:
Following is an example, which would sort the result in descending order by NAME:
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups.
The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.
Syntax:
The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the
conditions in the WHERE clause and must precede the ORDER BY clause if one is used.
Example:
Consider the CUSTOMERS table is having the following records:
If you want to know the total amount of salary on each customer, then GROUP BY query would
be as follows:
Now, let us have following table where CUSTOMERS table has the following records with
duplicate names:
Now again, if you want to know the total amount of salary on each customer, then GROUP BY
query would be as follows:
The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all
the duplicate records and fetching only unique records.
There may be a situation when you have multiple duplicate records in a table. While fetching
such records, it makes more sense to fetch only unique records instead of fetching duplicate
records.
Syntax:
Example:
Consider the CUSTOMERS table having the following records:
First, let us see how the following SELECT query returns duplicate salary records:
This would produce the following result where salary 2000 is coming twice which is a duplicate
record from the original table.
Now, let us use DISTINCT keyword with the above SELECT query and see the result:
This would produce the following result where we do not have any duplicate entry:
SQL- JOIN
The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN
is a means for combining fields from two tables by using values common to each.
Now, let us join these two tables in our SELECT statement as follows:
Here, it is noticeable that the join is performed in the WHERE clause. Several operators can be
used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be
used to join tables. However, the most common operator is the equal symbol.
The SQL JOIN syntax
Note: The INNER keyword is optional: it is the default as well as the most commonly used JOIN
operation.
SQL JOIN Examples
This will list all customers, whether they placed any order or not.
The ORDER BY TotalAmount shows the customers without orders first (i.e. TotalMount is
NULL).
SQL RIGHT JOIN
RIGHT JOIN performs a join starting with the second (right-most) table and then any
matching first (left-most) table records.
RIGHT JOIN and RIGHT OUTER JOIN are the same.
This returns suppliers that have no customers in their country, and customers that have no
suppliers in their country, and customers and suppliers that are from the same country.
SQL UNION Clause
UNION combines the result sets of two queries.
Column data types in the two queries must match.
UNION combines by column position rather than column name.
Sub queries can also assign column values for each record:
SQL Sub query Examples