28
Practical 1 Introduction to SQL SQL Structured Query Language is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS). Originally based upon relational algebra and tuple relational calculus , SQL consists of a data definition language and a data manipulation language . The scope of SQL includes data insert, query, update and delete , schema creation and modification, and data access control. Although SQL is often described as, and to a great extent is, a declarative language (4GL ), it also includes procedural elements. SQL was one of the first commercial languages for Edgar F. Codd 's relational model , as described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks." Despite not entirely adhering to the relational model as described by Codd , it became the most widely used database language. SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987. [12] Since then, the standard has been enhanced several times with added features. Despite these standards, code is not completely portable among different database systems, which can lead to vendor lock-in . The different makers do not perfectly adhere to the standard, for instance by adding extensions, and the standard itself is sometimes ambiguous. 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. 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. 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.

SQL Commands

Embed Size (px)

DESCRIPTION

This document is all about SQL Commands commonly used...

Citation preview

Page 1: SQL Commands

Practical 1

Introduction to SQL

SQL Structured Query Language is a special-purpose programming language designed for

managing data held in a relational database management system (RDBMS).

Originally based upon relational algebra and tuple relational calculus, SQL consists of a data

definition language and a data manipulation language. The scope of SQL includes data insert,

query, update and delete, schema creation and modification, and data access control. Although

SQL is often described as, and to a great extent is, a declarative language (4GL), it also

includes procedural elements.

SQL was one of the first commercial languages for Edgar F. Codd's relational model, as

described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data

Banks." Despite not entirely adhering to the relational model as described by Codd, it became the

most widely used database language.

SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of

the International Organization for Standardization (ISO) in 1987.[12]

Since then, the standard has

been enhanced several times with added features. Despite these standards, code is not completely

portable among different database systems, which can lead to vendor lock-in. The different

makers do not perfectly adhere to the standard, for instance by adding extensions, and the

standard itself is sometimes ambiguous.

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.

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.

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.

Page 2: SQL Commands

Practical 2

Basic SQL commands (create, drop, insert)

Create :- SQL Create is the command used to create data objects, including everything from

new databases and tables to views and stored procedures.

DROP :- SQL DROP is another command that removes data from the data store. The drop

command must be performed on SQL objects including databases, tables, table columns, and

SQL views. Dropping any of these objects removes them completely from your SQL application

and all data contained in any of the data objects dropped are lost forever.

Page 3: SQL Commands

Insert :- SQL tables store data in rows, one row after another. The Insert command is the

command used to insert new data into a table by specifying a list of values to be inserted into

each table column.

Inserting Single value

Inserting Multi values

Page 4: SQL Commands

Practical 3

Viewing commands (select, update)

Select :- SQL Select may be the most commonly used command by SQL programmers. It is used

to extract data from databases and tp present data in a user-friendly table called the result set.

Page 5: SQL Commands

Update :- SQL Update is the command used to update existing table rows with new data values.

Update is a very powerful command in the SQL world. It has the ability to update every single

row in a database with the execution of only a single query.

Page 6: SQL Commands

Practical 4

Modify structure of table (alter)

Alter :- SQL Alter is the command used to add, edit, and modify objects like tables, databases,

and views. Alter is the command responsible for making table column adjustments or renaming

table columns. New table columns can also be added and dropped from existing SQL tables.

Page 7: SQL Commands

Practical 5

Compound condition commands (and, or, in, not in, between, like, not like)

AND :- SQL AND links together two or more conditional statements for increased filtering when

running SQL commands. AND helps the developer query for very specific records while

answering questions like, “ I want to view all orders made by a certain customer AND made on a

special date”. There is no limit to the number if AND conditions that can be applied to a query

utilizing the WHERE clause. This makes it possible for the developer to be as precise as needed

when querying for results.

OR :- SQL OR also applies logic to help filter results. The difference is that instead of linking

together conditional statements, an OR condition just asks SQL to look for 2 separate conditions

within the same query and return any records/rows matching either of the conditions.

Page 8: SQL Commands

In :- SQL In is an operator used to pull data matching a list of values. A scenario where this

proves useful would be if we wanted to retrieve customer data for two or more customers.

Not In :- SQL Not In, allows the developer to eliminate a list of specific values from the result

set.

Page 9: SQL Commands

BETWEEN :- BETWEEN is a conditional statement found in the WHERE clause. It is used to

query for table rows that meets a condition falling between a specified range of numeric values.

BETWEEN essentially combines two conditional statements into one and simplifies the

querying process for you. To understand exactly what we mean, we could create another query

without using the BETWEEN condition and still come up with same results.

Page 10: SQL Commands

LIKE :-

NOT LIKE :-

Page 11: SQL Commands

Practical 6

Aggregate functions (sum, count, max, min, average)

Sum :-

Count :-

Max :-

Page 12: SQL Commands

Min :-

Average :-

Page 13: SQL Commands

Practical 7

Grouping commands (group by, order by)

GROUPBY :- SQL GROUP BY aggregates column values into a single record value. GROUP

BY requires a list of table columns on which to run the calculations. Here, SQL has consolidated

like values and returned those that are unique. In this case, we have actually duplicated the

behavior of SELECT DISTINCT, but you have also seen firsthand how GROUP BY accepts a

table column as a list and consolidates lie customer values. To unleash the true power of

GROUP BY, it is necessary to include at least one mathematical function, and to do so we will

utilize the SUM function to calculate how many total items have been purchased by each of our

customers.

Page 14: SQL Commands

Order By :- Order By is the SQL command used to sort rows as they are returned from a select

query. SQL order by command may be added to the end of any select query and it requires at

least one table column to be specified in order for SQL to sort the results.

Page 15: SQL Commands

Practical 8

Data constraint commands (primary key, foreign key)

Primary key

Page 16: SQL Commands

Foreign key

Page 17: SQL Commands

Practical 9

Renaming command

Page 18: SQL Commands

Practical 10

Join commands

Equi Join

Page 19: SQL Commands

Self Join

Page 20: SQL Commands

Practical 11

Set Operations

Union : SQL UNION combines two separate SQL queries into one result set. A JOIN

statement adds additional table columns to a result set, UNION combines row results from one

table with rows of another table.

Minus :

Page 21: SQL Commands

Practical 12

Scalar function and String functions

Lower

Upper

Page 22: SQL Commands

Initcap

Substr

Page 23: SQL Commands

Left Trim

Right Trim

Page 24: SQL Commands

Lpad

Rpad

Page 25: SQL Commands

Power

Square

Page 26: SQL Commands

Concatenation

Page 27: SQL Commands

Practical 13

Command for views

Describe

Page 28: SQL Commands

Practical 14

Check Constraints

Practical 15

Having Clause