SQL

Date post:25-Oct-2014
Category:
View:55 times
Download:1 times
Share this document with a friend
Transcript:

SQL IntroductionSQL stands for Structured Query Language. and it is generally referred to as SEQUEL. SQL is simple language to learn. SQL is a Nonprocedural language, as compared to the procedural or third generation languages (3GLs) such as COBOL and C. SQL was developed by IBM in the 1970s. The American National Standards Institute (ANSI) published its first SQL standard in 1986 and a second widely adopted standard in 1989. ANSI released updates in 1992, known as SQL92 and SQL2, and again in 1999, termed both SQL99 and SQL3. Each time, ANSI added new features and incorporated new commands and capabilities into the language. SQL is a simple, yet powerful, language used to create, access, and manipulate data and structure in the database.

SQL Statements categories: DDL - Data Definition Language.DDL is used to define, alter, or drop database objects and their privileges. DDL statements will implicitly perform a commit.

DDL Statements:Create Alter Drop Truncate It is used to create objects(tables, views) in the database. It is used to alter the structure of the database objects. delete database objects (It will invalidate the dependent objects ,it also drops indexes, triggers and referential integrity constraints ). remove all records from a table, including all spaces allocated for the records are removed (It is fast as compared to Delete and does not generate undo information as Delete does. It performs an implicit commit as it is a DDL. It resets the high water mark.) assigning privileges

Grant

DML - Data Manipulation Language.DML is used to access, create, modify or delete data in the structures of the database.

DML Statements:Select Insert Select data from the database It is used to insert data into a table

Update Delete

It is used to update existing data within a table It removes rows from the table.

DCL - Data Control LanguageFollowing are the examples of Data control Statements.

DCL Statements:Commit Savepoint Rollback SetTransaction It will end the current transaction making the changes permanent and visible to all users.. It will identify a point(named SAVEPOINT) in a transaction to which you can later roll back It will undo all the changes made by the current transaction. It is used to define the properties of a transaction.

SQL SyntaxOn this page, you will find SQL syntax for some of the most important SQL commands. These below SQL Syntaxes will be suitable for quick reference. SELECT [hint][DISTINCT] select_list FROM table_list [WHERE conditions] [GROUP BY group_by_list] [HAVING search_conditions] [ORDER BY order_list [ASC DESC] ] [FOR UPDATE for_update_options] SQL Select Statement SELECT "column name" FROM "table name" Example: Select salary from emp; SQL Where

SELECT "column name" FROM "table name" WHERE "condition" Example: Select salary from emp Where salary > 2000 SQL Distinct SELECT DISTINCT "column name" FROM "table name" Example: Select DISTINCT name from emp; SQL And/Or SELECT "column name" FROM "table name" WHERE "condition" {[ANDOR] "condition"}+ Example: SELECT salary FROM emp WHERE Salary > 1000 OR (Salary 275) SQL Between SELECT "column name" FROM "table name" WHERE "column name" BETWEEN 'value1' AND 'value2' Example: SELECT * FROM emp WHERE Date BETWEEN 'Jan-01-1999' AND 'Jan-15-1999' SQL In SELECT "column name" FROM "table name" WHERE "column name" IN ('value1', value2 ...)

Example: SELECT * FROM emp WHERE last_name IN ('sharma', 'dhall') SQL Like SELECT "column name" FROM "table name" WHERE "column name" LIKE {PATTERN} Example: SELECT * FROM emp WHERE last_name LIKE '%EN%' SQL Order By SELECT "column name" FROM "table name" [WHERE "condition"] ORDER BY "column name" [ASC, DESC] Example: SELECT name, Salary FROM emp ORDER BY name DESC SQL Count SELECT COUNT ("column name") FROM "table name" Example: SELECT COUNT (salary) FROM emp SELECT * FROM Table; In the above query, SELECT and FROM are SQL keywords, * is a wildcard which means all columns According to wikipedia Commonly available SQL keywords related to SELECT include:

1. FROM is used to indicate from which tables the data is to be taken, as well as how the tables JOIN to each other. 2. WHERE is used to identify which rows to be retrieved, or applied to GROUP BY. WHERE is evaluated before the GROUP BY. 3. GROUP BY is used to combine rows with related values into elements of a smaller set of rows. 4. HAVING is used to identify which of the "combined rows" (combined rows are produced when the query has a GROUP BY keyword or when the SELECT part contains aggregates), are to be retrieved. HAVING acts much like a WHERE, but it operates on the results of the GROUP BY and hence can use aggregate functions. 5. ORDER BY is used to identify which columns are used to sort the resulting data.

Returning a Single Column from a table The syntax is as follows: SELECT Column FROM Table; Oracle uses the ANSI standard concatenation operator, . Because this operator is reserved for string concatenation, the operands don't need to be cast to a string typethey'll be converted automatically: SELECT FirstName ' ' LastName AS Name FROM EMP_Database; Sorting with the ORDER BY Clause The ORDER BY clause is always applied after all other clauses are applied, such as the WHERE and GROUP BY clauses. Without an ORDER BY clause in an SQL statement, rows will often be retrieved in the physical order in which they were added to the table. The default behavior is to sort rows in ascending order. SELECT ColumnA, ColumnB FROM Table ORDER BY ColumnA ASC; Although this is the default behavior so you don't need to explicitly specify the ASC keyword, it's a better idea to include it to ensure that your queries are as easy to read as possible. Also, you may want to sort your rows in descending order, in which case you use the DESC keyword: SELECT ColumnA, ColumnB FROM Table ORDER BY ColumnA DESC; Filtering Data DISTINCT always retrieves the first value from a repeating group. If there are multiple repeating groups

DISTINCT will retrieve the first row from each group. Hence, DISTINCT will always require a sort. DISTINCT can operate on a single or multiple columns. SELECT DISTINCT ColumnA FROM Table; In order to filter out duplicate rows, you use the DISTINCT keyword. If you want to retrieve multiple columns, you can guarantee that every row you obtain contains unique data for the specified set of columns. For example, the following query will return only unique combinations of customer names and debit card details: SELECT DISTINCT CustomerName, DebitCard FROM Customers; This doesn't mean that you won't have duplicates in either column, only that each combination is unique. You could, for example, have several customers with the same name but with different debit card numbers. Using WHERE Clause We can use the WHERE clause to restrict the rows returned by a query. SELECT ColumnA, ColumnB, ColumnC FROM Table WHERE Condition; Condition is very flexible, allowing you to test for equalities and inequalities in column data, ranges of values to look for etc. You achieve all this using a simple syntax that includes various operators and keywords that, when combined, allow you to search for pretty much anything. We can use following comparison operators with the WHERE clause:

Operator = , !=, ^= >

Meaning Equal Not Equal Greater Than

< >= 7 ; 3) Find the average price of Bud. Sells (bar, beer, price) SELECT AVG (price) FROM Sells WHERE beer = 'Bud';

SQL SELECTSQL SELECT The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set.

SQL SELECT Syntax SELECT column_name(s) FROM table_name [Source] The SQL statement below shows a simple usage of the SQL SELECT command: SELECT FirstName, LastName, DateOfBirth FROM Employees The SELECT statement has many optional clauses: WHERE specifies which rows to retrieve. GROUP BY groups rows sharing a property so that an aggregate function can be applied to each group. HAVING selects among the groups defined by the GROUP BY clause. ORDER BY specifies an order in which to return the rows.

SQL InsertInserting a new row into the dept table insert into dept (deptno, dname, loc) values (10,'Apple','Boston')

You can omit the column list in the insert statement but then you have to enter those in the same order as they appear in the table and you have o include all the columns in the values. The following example illustrates the use of the "default" keyword while inserting the records.insert into table_name values(default);

orinsert into table_name(column1,column2..) values(default) How to create a new table having the same structure as some other table? create table new_table as select * from old_table

How to insert data into multiple tables?

There are two terms used INSERT ALL AND INSERT FIRSTEXAMPLE:

Suppose there are three tables emp,emp_1 and emp_2 with the same sructure and columns.insert all when sal in (800,1600) then into emp (empno,ename,job) values (empno,ename,job) when sal = 3200 then into emp_1 (empno,ename,job) values (empno,ename,job) else into emp_2 (empno,ename,job) values (empno,ename,job) select empno,ename,job from emp

INSERT FIRST will breakout as soon as it sees a condition that evaluates to true. INSERT ALL will evaluate al the conditions even if the previous condition evluates to true.Insert Default Values

Some RDBMSs, including SQL Server, allow you to create a new row based on default values that are defined in the table specification using the DEFAULT VALUES method in place of the VALUES section. Suppose you have a table like this:CREATE TABLE A ( A_ID int IDENTITY PRIMARY KEY, Name varchar(100) DEFAULT 'Anonymous', Dates varchar(20) DEFAULT 'Unknown);

and you want to insert a new row with just the default values f

Popular Tags:

Click here to load reader

Embed Size (px)
Recommended