Pages

SQl Tutorial and various Quiries

                    How to Create a table in SQL

General syntax for creating a Table is as follows

create table table_name(field1_name datatype(size), field2_name datatype(size),...........);

Eg,

CREATE TABLE SALESMAN
(SNUM NUMBER(4) PRIMARY KEY,
SNAME VARCHAR(20),
CITY VARCHAR(10),
COMMISSION NUMBER(6,2));

this query will create a table name SALESMAN


See the structure of salesman table

How can we see the structure of the table which we created previously.
Have look at the query below.

DESC SALESMAN;

Insert values into table salesman

There are two ways in which we can insert the values in any table.

INSERT INTO SALESMAN VALUES(1001,'PIYUSH','LONDON',.12);

And the second way is

INSERT INTO SALESMAN VALUES(&SNUM,'&SNAME','&CITY',&COMMISSION);

This will generate the following output:-
Enter value for snum: 1001
Enter value for sname: PIYUSH
Enter value for city: LONDON
Enter value for commission: .11

and the insert the following value

SNUM SNAME          CITY              COMMISSION
--------------------------------------------------------  
    1001 PIYUSH        LONDON            12%
    1002 NIRAJ           SURAT                 13%
    1003 MITI              LONDON            11%
    1004 RAJESH        BARODA            15%
    1005 ANAND        NEW DELHI       10%
    1006 RAM              PATAN                  10%
    1007 LAXMAN     BOMBAY              09%

As said earlier create a similar table named as Customer

CREATE TABLE CUSTOMER
(CNUM NUMBER(4) PRIMARY KEY,
 CNAME VARCHAR2(20),
CITY VARCHAR2(25),
RATING NUMBER(3),
SNUM NUMBER(3) CONSTRAINT SNUM_FKEY REFERENCES SALESMAN);

And insert the following values into Customer Table.


CNUM CNAME         CITY     RATING     SNUM
--------------------------------------------------------  
    2001 HARDIK           LONDON    100      1001
    2002 GITA               ROME         200      1003
    2003 LAXIT           SURAT        200      1002
    2004 GOVIND       BOMBAY    300     1002
    2005 CHANDU      LONDON    100      1001
    2006 CHAMPAK   SURAT        300     1007
    2007 PRATIK        ROME          100      1004

Query in our lab manual

Hey friends today I will solve the query in our labmanual......

1. Produce the order no ,amount and date of all orders.


2. Give all the information about all the customers with salesman number 1001.


3. Display the information in the sequence of city, sname, snum, and Commission.


4. List of rating followed by the name of each customer in Surat.


5. List of snum of all salesmen with orders in order table without an duplicates.


6. List of all orders for more than Rs. 1000.


7. List out names and cities of all salesmen in London with commission above 10%


8. List all customers excluding those with rating <= 100 or they are located in Rome.


9. List all order for more than Rs. 1000 except the orders of snum,1006 of 10/03/99

You can write the same  Query in two different ways.

10. List all orders taken on 10 of March,April and May 1999.


11. List all customers whose names begins with a letter 'C'.


12. List all customers whose names begins with letter 'A' to 'G'


13. List all orders with zero or NULL amount.


14. Find out the largest orders of salesman 1002 and 1007.


15. Count all orders of 10-Mar-99.


16. Calculate the total amount ordered.


17. Calculate the average amount ordered.


18. Count the no. of salesmen currently having orders.