SQL
SQL
SQL
SQL tutorial provides basic and advanced concepts of SQL. Our SQL tutorial is designed for beginners
and professionals.
SQL (Structured Query Language) is used to perform operations on the records stored in database
such as updating records, deleting records, creating and modifying tables, views etc.
SQL is just a query language, it is not a database. To perform SQL queries, you need to install any
database for example Oracle, MySQL, MongoDB, PostGre SQL, SQL Server, DB2 etc.
SQL DATABASE
SQL TABLE
SQL SELECT
SQL SELECT statement with UNIQUE, DISTINCT, AS and IN keywords and first(), last(), top(), sum(),
random() and count() functions.
SQL INSERT
SQL UPDATE
SQL DELETE
SQL DELETE TABLE, DELETE One Row, DELETE all rows, DELETE VIEW, DELETE database, DELETE
duplicate rows and DELETE with INNER JOIN.
Our SQL join tutorial provides full detail of INNER JOIN, OUTER JOIN, LEFT OUTER JOIN, RIGHT
OUTER JOIN and SELF JOIN.
SQL Keys
SQL keys are primary key, composite key, foreign key, alternate key and unique key. In SQL keys
tutorial, we will provide description and examples of each key.
SQL QUIZ
What is SQL
All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, Postgres and SQL Server use SQL as
standard database language.
SQL is required:
o With SQL, we can query our database in a numbers of ways, using English-like statements.
o With SQL, user can access data from relational database management system.
o It allows user to describe the data.
o It allows user to define the data in database and manipulate it when needed.
o It allows user to create and drop database and table.
o It allows user to create view, stored procedure, function in a database
o It allows user to set permission on tables, procedure and view.
What is Database
Database handlers create database in such a way that only one set of software program provide
access of data to all the users.
The main purpose of database is to operate large amount of information by storing, retrieving and
managing.
There are many dynamic websites on the world wide web now a days which are handled through
databases. For example, a model to checks the availability of rooms in a hotel. It is an example of
dynamic website that uses database.
There are many database available like MySQL, Sybase, Oracle, Mango DB, Informix, Postgre, SQL
Server etc.
SQL or Structured Query Language is used to perform operation on the data stored in a database.
SQL depends on relational algebra and tuple relational calculus.
What is RDBMS
All modern database management systems like SQL, MS SQL Server, IBM DB2, ORACLE, My-SQL and
Microsoft Access are based on RDBMS.
It is called Relational Data Base Management System (RDBMS) because it is based on relational model
introduced by E.F. Codd.
How it works
Relational database is most commonly used database. It contains number of tables and each table has
its own primary key.
Due to a collection of organized set of tables, data can be accessed easily in RDBMS.
Brief History of RDBMS
During 1970 to 1972, E.F. Codd published a paper to propose the use of relational database model.
What is table
The RDBMS database uses tables to store data. A table is a collection of related data entries and
contains rows and columns to store data.
1 Ajeet 24 B.Tech
2 aryan 20 C.A
3 Mahesh 21 BCA
4 Ratan 22 MCA
5 Vimal 26 BSC
What is field
Field is a smaller entity of the table which contains specific information about every record in the
table. In the above example, the field in the student table consist of id, name, age, course.
A row of a table is also called record. It contains the specific information of each individual entry in the
table. It is a horizontal entity in the table. For example: The above table contains 5 records.
1 Ajeet 24 B.Tech
What is column
A column is a vertical entity in the table which contains all information associated with a specific field
in a table. For example: "name" is a column in the above table which contains all information about
student's name.
Ajeet
Aryan
Mahesh
Ratan
Vimal
NULL Values
The NULL value of the table specifies that the field has been left blank during record creation. It is
totally different from the value filled with zero or a field that contains space.
Data Integrity
There are the following categories of data integrity exist with each RDBMS:
Domain integrity: It enforces valid entries for a given column by restricting the type, the format, or
the range of values.
Referential integrity: It specifies that rows cannot be deleted, which are used by other records.
User-defined integrity: It enforces some specific business rules that are defined by users. These
rules are different from entity, domain or referential integrity.
The main differences between DBMS and RDBMS are given below:
No. DBMS RDBMS
1) DBMS applications store data as file. RDBMS applications store data in a tabular form.
2) In DBMS, data is generally stored in either a In RDBMS, the tables have an identifier called primary key and the
hierarchical form or a navigational form. data values are stored in the form of tables.
4) DBMS does not apply any security with RDBMS defines the integrity constraint for the purpose of ACID
regards to data manipulation. (Atomocity, Consistency, Isolation and Durability) property.
5) DBMS uses file system to store data, so there in RDBMS, data values are stored in the form of tables, so
will be no relation between the tables. arelationship between these data values will be stored in the form
of a table as well.
6) DBMS has to provide some uniform methods RDBMS system supports a tabular structure of the data and a
to access the stored information. relationship between them to access the stored information.
8) DBMS is meant to be for small organization RDBMS is designed to handle large amount of data. it
and deal with small data. it supportsmultiple users.
supports single user.
9) Examples of DBMS are file systems, xml etc. Example of RDBMS are mysql, postgre, sql server, oracle etc.
After observing the differences between DBMS and RDBMS, you can say that RDBMS is an extension
of DBMS. There are many software products in the market today who are compatible for both DBMS
and RDBMS. Means today a RDBMS application is DBMS application and vice-versa.
SQL Syntax
SQL follows some unique set of rules and guidelines called syntax. Here, we are providing all the basic
SQL syntax.
o SQL is not case sensitive. Generally SQL keywords are written in uppercase.
o SQL statements are dependent on text lines. We can place a single SQL statement on one or
multiple text lines.
o You can perform most of the action in a database with SQL statements.
o SQL depends on relational algebra and tuple relational calculus.
SQL statement
SQL statements are started with any of the SQL commands/keywords like SELECT, INSERT, UPDATE,
DELETE, ALTER, DROP etc. and the statement ends with a semicolon (;).
Semicolon is used to separate SQL statements. It is a standard way to separate SQL statements in a
database system in which more than one SQL statements are used in the same call.
In this tutorial, we will use semicolon at the end of each SQL statement.
SQL Commands
In a database table, every column is required to have a name and a data type.
Data Type varies from database to database. For example, MySQL supports INT but Oracle supports
NUMBER for integer values.
Numeric NUMERIC(P,S) where 'p' is precision value and 's' is scale value.
Decimal DECIMAL(P,S) where 'p' is precision value and 's' is scale value.
Bit varying BIT VARYING(X) 'x' is the number of bits to store (length can vary up to x)
Timestamp TIMESTAMP it stores year, month, day, hour, minute and second values
Time with TIME WITH exactly same as time but also store an offset from UTC
timestamp with TIMESTAMP WITH same as timestamp but also stores an offset from UTC of
Let's assume two variables "a" and "b". Here "a" is valued 50 and "b" valued 100.
Example:
+ It is used to add containing values of both operands a+b will give 150
- It subtracts right hand operand from left hand operand a-b will give -50
/ It divides left hand operand by right hand operand b/a will give 2
% It divides left hand operand by right hand operand and returns reminder b%a will give 0
= Examine both operands value that are equal or not,if yes condition become true. (a=b) is not
true
!= This is used to check the value of both operands equal or not,if not condition become true. (a!=b) is true
<> Examines the operand?s value equal or not, if values are not equal condition is true (a<>b) is true
> Examine the left operand value is greater than right Operand, if yes condition becomes true (a>b) is not
true
< Examines the left operand value is less than right Operand, if yes condition becomes true (a<="" td="">
>= Examines that the value of left operand is greater than or equal to the value of right operand (a>=b) is not
or not,if yes condition become true true
<= Examines that the value of left operand is less than or equal to the value of right operand or (a<=b) is true
not, if yes condition becomes true
!< Examines that the left operand value is not less than the right operand value (a!<=""
td="">
!> Examines that the value of left operand is not greater than the value of right operand (a!>b) is true
Let's take two variables "a" and "b" that are valued 50 and 100.
Operator Description
ALL this is used to compare a value to all values in another value set.
AND this operator allows the existence of multiple conditions in an SQL statement.
ANY this operator is used to compare the value in list according to the condition.
BETWEEN this operator is used to search for values, that are within a set of values
NOT the NOT operator reverse the meaning of any logical operator
EXISTS the EXISTS operator is used to search for the presence of a row in a specified table
LIKE this operator is used to compare a value to similar values using wildcard operator