Você está na página 1de 3

dvance query Advance queries & sub queries.

One of the mast powerful features of the SQL SELECT COMMAND is known as a sub query or nested query. This feature enables as to ask complex questions that initial retrieving different types of data from different sources. SQL is also more than a query language. It can be used to create the entire database. SQL also has powerful commands to alter the data. Two key points will help we learn hew to use sub queries. i) SQL was design to work with sets of data avoid thinking in terms of individual rows. ii) We can start nested queries into their separate parts & deal with the part individually. Sub query with ANY & ALL. The ANY & ALL operators combine comparison of numbers with subsets. In the preceding section, the IN operator compared a value to a list of items in a set - however, the comparison was based on equality. The test item had to exactly match an entity in the list. The ANY & all operators work with less than (<) or greater than (>) & compare the test value to a list of values.

1) Data definition language (DDL)

DDA is used to create & remove data box objects, for e.g.- create, alter, drop etc.

a) Creating a table using SQL. (CR EATE)

In order to create a table the DDL CREATE COMMAND is used & columns of the table along with data type & the width specified. Syntax: CREATE TABLE <table_Name> ( Field-name 1 field-type (width); Field-name 2 field-type (width); ..................................................... ..................................................... Field-name field-type (width) );

b) Insert data into a table.

We can insert required data in to specified table by using following query. INSERT TNTO <table-Name> VALUE (v1, v2 ...vn) e.g. INSERT INTO student Values (0001, ram, 2010-10-10)

The DDL ALTER command can be used for the following changes to any table. i) To add new column. ii) To add new integrity constraints. iii) To modify existing columns. To expand length.

To change default. To decrease length but all values in the column must be null. To change data type but all values in the column must be null.

2) Data manipulation language. (DML)

DML is used to manipulate data in database. Some commands are: INSERT, DELETE, UPDATE & SELECT.

i) SQL query for INSERT.

To add data to the database we use following query syntax. INTSERT TNTO<table name > (columns to insert) E.g. INSERI INTO Student (Roll-no. name, address) VALUES (005, Hari, NPJ); ii) Like operator. We use like, pattern where the pattern can contain wildcard character like. a) % (percentage) b) - (underscore) Here % is used to denote any number of unknown characters & - denotes one unknown character. If we want to denote more than one unknown character using then the number of - must be equal to the number of unknown character. e.g. SELECT * FROM student WHERE Name like (D %); Retrieve all the rows where name begins with D following by any other character. e.g. Use of underscore (-) SELECT * FROM student WHERE ADD like D-a;

iii) DELETE command:

To DELETE rows from a table, we use the delete command as follow. Syntax 1. DELETE FROM <table-name>; Syntax 2: DELETE FROM <table -name > where <condition> e.g. i) DELETE FROM student; ii) DELETE FROM student WHERE Name=Ram;

v) UPDATE COMMAND:To UPDATE rows in the table, we use update command as follows. Syntax 1: UPDATE <table-Name> SET <field-Name >=value Syntax 2: UPDATE <table name> SET<field name>=value; WHERE <FIELD Name>=value;

e.g. UPDATE student. ROLL-NO =001 UPDATE student

Roll-No =001 Where name =Krishna ;