SQL Introduction
SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English Query Language)”. It is a query language used for accessing and modifying information in the database. IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard. It has become a Standard Universal Language used by most of the relational database management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft SQL server, Sybase etc. Most of these have provided their own implementation thus enhancing it's feature and making it a powerful tool. Few of the sql commands used in sql programming are SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement, WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views, GROUP Functions, Indexes etc.In a simple manner, SQL is a non-procedural, English-like language that processes data in groups of records rather than one record at a time. Few functions of SQL are:
- store data
- modify data
- retrieve data
- modify data
- delete data
- create tables and other database objects
- delete data
History
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's original quasi-relational database management system, System R, which a group at IBM San Jose Research Laboratory had developed during the 1970s. The acronym SEQUEL was later changed to SQL because "SEQUEL" was a trademark of the UK-based Hawker Siddeley aircraft company.The first Relational Database Management System (RDBMS) was RDMS, developed at MIT in the early 1970s, soon followed by Ingres, developed in 1974 at U.C. Berkeley. Ingres implemented a query language known as QUEL, which was later supplanted in the marketplace by SQL.
In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the concepts described by Codd, Chamberlin, and Boyce and developed their own SQL-based RDBMS with aspirations of selling it to the U.S. Navy, Central Intelligence Agency, and other U.S. government agencies. In June 1979, Relational Software, Inc. introduced the first commercially available implementation of SQL, Oracle V2 (Version2) for VAX computers. Oracle V2 beat IBM's August release of the System/38 RDBMS to market by a few weeks.
After testing SQL at customer test sites to determine the usefulness and practicality of the system, IBM began developing commercial products based on their System R prototype including System/38, SQL/DS, and DB2, which were commercially available in 1979, 1981, and 1983, respectively.
The SQL language is subdivided into several language elements, including:
- Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)
- Expressions, which can produce either scalar values or tables consisting of columns and rows of data.
- Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow.
- Queries, which retrieve the data based on specific criteria. This is the most important element of SQL.
- Statements, which may have a persistent effect on schemata and data, or which may control transactions, program flow, connections, sessions, or diagnostics.
- SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
- Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
Queries
The most common operation in SQL is the query, which is performed with the declarativeSELECT statement. SELECT retrieves data from one or more tables, or expressions. Standard SELECT statements have no persistent effects on the database. Some non-standard implementations of SELECT can have persistent effects, such as the SELECT INTO syntax that exists in some databases.Queries allow the user to describe desired data, leaving the database management system (DBMS) responsible for planning, optimizing, and performing the physical operations necessary to produce that result as it chooses.
A query includes a list of columns to be included in the final result immediately following the
SELECT keyword. An asterisk ("*") can also be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include:- The
FROMclause which indicates the table(s) from which data is to be retrieved. TheFROMclause can include optionalJOINsubclauses to specify the rules for joining tables. - The
WHEREclause includes a comparison predicate, which restricts the rows returned by the query. TheWHEREclause eliminates all rows from the result set for which the comparison predicate does not evaluate to True. - The
GROUP BYclause is used to project rows having common values into a smaller set of rows.GROUP BYis often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. TheWHEREclause is applied before theGROUP BYclause. - The
HAVINGclause includes a predicate used to filter rows resulting from theGROUP BYclause. Because it acts on the results of theGROUP BYclause, aggregation functions can be used in theHAVINGclause predicate. - The
ORDER BYclause identifies which columns are used to sort the resulting data, and in which direction they should be sorted (options are ascending or descending). Without anORDER BYclause, the order of rows returned by an SQL query is undefined.
Data types
Each column in an SQL table declares the type(s) that column may contain. ANSI SQL includes the following data types.Character strings
CHARACTER(n)orCHAR(n)— fixed-width n-character string, padded with spaces as neededCHARACTER VARYING(n)orVARCHAR(n)— variable-width string with a maximum size of n charactersNATIONAL CHARACTER(n)orNCHAR(n)— fixed width string supporting an international character setNATIONAL CHARACTER VARYING(n)orNVARCHAR(n)— variable-widthNCHARstring
Bit strings
BIT(n)— an array of n bitsBIT VARYING(n)— an array of up to n bits
Numbers
INTEGERandSMALLINTFLOAT,REALandDOUBLE PRECISIONNUMERIC(precision, scale)orDECIMAL(precision, scale)
SQL provides a function to round numerics or dates, called
TRUNC (in Informix, DB2, PostgreSQL, Oracle and MySQL) or ROUND (in Informix, Sybase, Oracle, PostgreSQL and Microsoft SQL Server).Date and time
DATE— for date values (e.g.,2011-05-03)TIME— for time values (e.g.,15:51:36). The granularity of the time value is usually a tick (100 nanoseconds).TIME WITH TIME ZONEorTIMETZ— the same asTIME, but including details about the time zone in question.TIMESTAMP— This is aDATEand aTIMEput together in one variable (e.g.,2011-05-03 15:51:36).TIMESTAMP WITH TIME ZONEorTIMESTAMPTZ— the same asTIMESTAMP, but including details about the time zone in question.