SQL
What is SQL?
SQL is structured Query Language which is a computer language for storing, manipulating and
retrieving data stored in relational database.
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 uses SQL as
standard database language.
Also they are using different dialects, Such as:
+ MS SQL Server using T-SQL,
+ Oracle using PLSQL,
+ MS Access version of SQL is called JET SQL (native format Jete
Why SQL?
+ Allow users to access data in relational database management systems.
+ Allow users to describe the data.
«Allow users to define the data in database and manipulate that data.
+ Allow to embed within other languages using SQL modules, libraries & pre-compilers.
+ Allow users to create and drop databases and tables.
+ Allow users to create view, stored procedure, functions in a database
+ Allow 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.
Following is a simple diagram showing SQL. Architecture:SQL DATABASE :
MySQL
MySQL is open source SQL database, which is developed by Swedish company MySQL AB.
MySQL is pronounced "my ess-que-ell,” in contrast with SQL, pronounced "sequel."
MySQL is supporting many different platforms including Microsoft Windows, the major Linux
distributions, UNIX, and Mac OS X.
MySQL has free and paid versions, depending on its usage (non-commercial/commercial) and
features. MySQL comes with a very fast, multi-threaded, multi-user, and robust SQL database
server.
Features:
High Performan
High Availability.
Scalability and Flexibility Run anything,
Robust Transactional Support.
Web and Data Warehouse Strengths.
Strong Data Protection.
Comprehensive Application Development.
Management Ease.
Open Source Freedom and 24 x 7 Support.
Lowest Total Cost of Ownership.SQL Arithmetic Operators:
Assume variable a holds 10 and variable b holds 20 then;
Operator Description Example
Addition - Adds values on cither side of the
. operator a+b will give 30
Subtraction - Subtracts right hand operand from
left hand operand Seperate
Multiplication - Muliplies values on ether side «5 i give 200
of the operator
Division - Divides left hand operand by right
hhand operand b/awill give 2
Modulus - Divides left hand operand by right
hand operand and returns remainder b% awlll give 0
SQL Comparison Operators:
Assume variable a holds 10 and variable b holds 20 then:
Operator Description Example
Checks if the value of two operands are equal or
- a =b) isnot true.
not. if yes then condition becomes true. 2)
Checks if the value of two operands are equal or
Ie not, if values are not equal then condition (a tb) is true.
becomes true,
Checks ifthe value of two operands are equal or
° not, if values are not equal then condition (a d)is true
becomes truebs
SOL Log
Checks if the value of left operand is greater
than the value of right operand, if yes then (a> b) is not te.
condition becomes true.
Checks if the value of left operand is less than
the value of right operand, if yes then condition (a = b) is not true
then condition becomes true,
Checks if the value of left operand is less than or
equal to the value of right operand, if yes then (a <= b) is true.
condition becomes true.
Checks if the value of left operand is not less
than the value of right operand, if yes then (a t bp is true.
condition becomes true.
1 Operators:
Here is a list of all the logical operators available in SQL.
Operator
ALL
AND
ANY
BETWEEN
Description
‘The ALL operator is used to compare a value to all values in another value set
‘The AND operator allows the existence of multiple conditions in an SQL statement’s
WHERE clause,
‘The ANY operator is used to compare a value to any applicable value in the list
according to the condition.
‘The BETWEEN operator is used to search for values that are within a set of values,
given the minimum value and the maximum value,waste ‘The EXISTS operator is used to search for the presence of a row in a specified table
that meets certain criteria.
‘The IN operator is used to compare a value to a list of literal values that have been
specified.
‘The LIKE operator is used to compare a value to similar values using wildcard
operators.
‘The NOT operator reverses the meaning of the logical operator with which it is used.
Eg. NOT EXISTS, NOT BETWEEN, NOT IN etc. This is negate operator.
oR ‘The OR operator is used to combine multiple conditions in an SQL. statement’s
WHERE clause,
Is NULL ‘The NULL operator is used to compare a value with a NULL value.
‘The UNIQUE operator searches every row of a specified table for uniqueness (no
1B
Uae duplicates)
COMMANDS IN SQL :
1, CREATE DATABASE
The SQL CREATE DATABASE statement is used to create new SQL database.
Syntax:
Basic syntax of CREATE DATABASE statement is as follows:
CREATE DATABASE DatabaseName;
Always database name should be unique within the RDBMS.
Example:
If you want to create new database , then CREATE DATABASE statement would be as
follows:
‘SQL> CREATE DATABASE testDB;
2. DROP DATABASEThe SQL DROP DATABASE statement is used to drop any existing database in SQL schema.
Syntax:
Basic syntax of DROP DATABASE statement is as follows:
DROP DATABASE DatabaseName;
Always database name should be unique within the RDBMS.
Example:
If you want to delete an existing database , then DROP DATABASE statement would be
as follows:
SQL> DROP DATABASE testDB;
3. USE
‘The SQL USE statement is used to select any existing database in SQL schema,
Syntax:
Basic syntax of USE statement is as follows:
USE DatabaseName;
4. CREATE TABLE
‘The SQL CREATE TABLE statement is used to create a new table.
Syntax:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE table_name(
columal datatype,
datatype,
datatype,
columaN datatype,
PRIMARY KEY( one or more columns )
»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.
A copy of an existing table can be created using a combination of the CREATE TABLE statement
and the SELECT statement.
5. DROP 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,
Syntax:
Basic syntax of DROP TABLE statement is as follows:
DROP TABLE table_name;
6, INSERT INTO
‘The SQL INSERT INTO Statement is used to add new rows of data to a table in the database,
Syntax:
‘There are two basic syntax of INSERT INTO statement is as follows:
INSERT INTO TABLE_NAME (column!, column?, column3,..columnN)]
VALUES (value1, value2, value3...valueN);
Here column!, 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 column(s) 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
the table, The SQL INSERT INTO syntax would be as follows:
the same order as the columns in
INSERT INTO TABLE_NAME VALUES7. SELECT
SQL SELECT Statement is used to fetch the data from a database table which returns data in the
form of result table. These result tables are called result-sets.
Syntax:
‘The basic syntax of SELECT statement is as follows
SELECT column! column2, column FROM table_name;
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 field then you can use following syntax:
SELECT * FROM table_name:
Example:
Consider CUSTOMERS table is having following records:
ID|NAME | AGE | ADDRE’
SALARY
1 Ramesh | 32 Ahmedabad | 2000.00 |
2(Khilan | 25| Delhi | 1500.00 |
3 |kaushik | 23] Kota | 2000.00.
4 |Chaitali | 25) Mumbai | 6500.00
5 |Hardik | 27| Bhopal | 8500.00 |
6|Komal | 22|MP | 4500.00)
7|Muffy | 24| Indore | 1000.00 |
Following is an example which would fetch ID, Name and Salary fields of the customers available in
CUSTOMERS table:
‘SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
This would produce following result:
[SALARY |
1 | Ramesh | 2000.00
2(Khilan | 1500.00 |3 | kaushik | 2000.00 |
4 | Chaitali | 6500.00 |
5 | Hardik | 8500.00 |
6|Komal | 4500.00 |
7|Muffy | 1000.00 |
8, WHERE CLAUSE
‘The SQL WHERE clause is used to specify a condition while fetching the data from single table or
joining with multiple table.
If the given condition is satisfied then only it returns specific value from the table. You would use
WHERE clause to filter the records and fetching only necessary records.
‘The WHERE clause not only used in SELECT statement, but itis also used in UPDATE, DELETE
statement etc. which we would examine in subsequent chapters,
Syntax:
The basie syntax of SELECT statement with WHERE clause is as follows:
SELECT column column?, column
FROM table_name
WHERE (condition)
You can specify a condition using comparision or logical operators like >, <, =, LIKE, NOT etc.