Vous êtes sur la page 1sur 15

SQL ASSIGNMENTS

AIM-Create the following tables in SQL and write the


SQL Queries related to it.
EmpNo

Ename

Job

MGR

Hiredate
17-Dec80
20-Feb81
22-Feb81
2-Apr-81
28-Sep81
1-May81
9-Jun-81
19-Apr81
17-Nov81
8-Sep-81
23-May81
3-Dec-81
3-Dec-81
23-Jan82

7369

SMITH

CLERK

7902

7499

ALLEN

SALESMAN

7698

7521
7566

WARD
JONES

SALESMAN
MANAGER

7698
7839

7654

MARTIN

SALESMAN

7698

7698
7782

BLAKE
CLARK

MANAGER
MANAGER

7839
7839

7788

SCOTT

ANALYST

7566

7839
7844

KING
TURNER

PRESIDENT
SALESMAN

7698

7876
7900
7902

ADAMS
AMES
FORD

CLERK
CLERK
ANALYST

7788
7698
7566

7934

MILLER

CLERK

7782

Dept No

Dname

10
20
30
40

ACCOUNTING
RESEARCH
SALES
OPERATIONS

LOC
NEW
YORK
DALLAS
CHICAGO
BOSTON

PNO
P1
P1
P1
P2

EMPNO
7379
7566
7788
7369

WORK_HRS
10
12
8
8

PNO
P1
P2
P3
P4

P2
P3
P4

7902
7788
7499

15
24
22

P5
P6
P7

Sal

Comm

800

DeptNo
20

26000

2600

10

28000
42000

2800
5000

50
40
20

38000
40000

5000
5000

20
30
40

95000
32000

3200

50
40

18000
21000
23000

50
30
50

15000

10

PNAME
Amarpali
Maruti
Intel
Gaur City
Godrej
City
Ford
GE

BUDGET
85000
250000
60000
25000

S_DATE
12/1/2013
1/2/2013
21/03/2013
1/7/2015

35000
125000
25000

1/8/2015
19/02/2014
13/07/2015

P5
P4
P5
P6
P6
P7
P8
P9

7900
7566
7654
7782
7876
7900
7876
7934

14
24
22
15
36
56
15
18

P8

Kirloskar

75000

VARIOUS COMMAND STATEMENT


CREATE COMMAND - The CREATE TABLE statement is used
to create a table in a database.

INSERT STATEMENT- The INSERT INTO statement is used


to insert new records in a table.

INSERT SYNTAX1. INSERT INTO table_name VALUES


value1,value2,value3,...);
2. INSERT INTO table_name VALUES
(&value1,&value2,&value3,&Value4,..);

SELECT Statement- The SELECT statement is used to


select data from a database.

SQL SELECT Syntax


SELECT column_name,column_name
FROM table_name;
And
SELECT * FROM table_name;

1/2/2014

SQL QUESTIONS WITH QUERIES


WHERE STAEMENT- The WHERE clause is used to filter
records.

WHERE Syntax
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

Operator
=
<>
>
<
>=

Description
Equal
Not equal. Note: In some versions of SQL this
operator may be written as !=
Greater than
Less than
Greater than or equal

<=
BETWEEN
LIKE
IN

Less than or equal


Between an inclusive range
Search for a pattern
To specify multiple possible values for a column

Q1. Display all Emp working as CLERK.

Q2. Display all Emp working in Dept10.

Q3.Display all Emp Working as CLERK or in DeptNo10.

Q4. Display all Emp Working as CLERK in DeptNo10.

Q5. Display all Emp Working as CLERL But not in DeptNo=10.

Q6.Display all Emp Working as either SALESMAN or Manager


but not in DeptNo-20.

Q7.Display all Emp who are not getting any commission.

Q8.Display all Emp whose salary is more than 1000 and


commission<1000.

DISPLAY ALL DEPT


Q1. Disaplay all Dept Located in NEW YORK.

Q2. Display all DeptNo more than 20.

PATTERN MATCHING
SQL LIKE Operator
LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

Percent(%)-The percent (%) character matches any one


string.
Underscore(_) The _ character matches any one
character.

Q1.Display all Emp whose name begin with A.

Q2.Display all Emp whose name end with A.

Q3. Display all Emp whose Name has 4 characters.

Q4.Display all Emp whose date of join is in the year 1981.

Q5.Display all Emp whose date of Join is in month of Feb.

AGGREGATE FUNCTIONS
The Summary values are calculated from the data in
particular column using SQL aggregate functions.They can
also be applied to to all rows in a table or to a subset of the
table specified by a WHERE Clause.SQL includes following
functions.

Avg - to compute average


Stddev to find the standard deviation
Min- to find the minimum value
Max- to find the maximum values.
Count- to count non null values
Count(*)-to count total number of rows in a table
Sum-to find total values
Variance-to compute the variance of rows in column.

GROUP BY Statement- The GROUP BY statement is


used in conjunction with the aggregate functions to
group the result-set by one or more columns.

GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Q1. Find the Total Salary of all Emp.

Q2. Find the Max,Min,Avg Sal of all Emp.

Q3. Find the Max,Min,Avg Sal Job-wise.

Q4. Find the Max,Min,Avg Sal dept-wise.

GROUP BY
GROUP BY Statement- The GROUP BY statement is
used in conjunction with the aggregate functions to
group the result-set by one or more columns.

GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

ORDER BY Syntax
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Q1.Dept-wise max,min,avg salary of Emp.

Q2. Job-wise max,min,avg salary of Emp.

Q3. Dept-wise max,min,avg salary in the reverse order of


DeptNo.

Q4.Job-wise max,min,avg salary in the ascending order of


JOB.

NESTED QUERY
Q1.Display the name of emp getting max salary.

Q2.Name of Emp getting max salary working as SALESMAN.

Q3.Name of Emp getting salary more than the avg sal of


SALESMAN.

Q4.Name of Emp getting salary more than the avg sal of all
emp in DeptNo10.

JOIN QUERY
Joins Statement- An SQL JOIN clause is used to combine rows from two or
more tables, based on a common field between them.The most common type of
join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all
rows from multiple tables where the join condition is met.
Different SQL JOINs
Before we continue with examples, we will list the types of the different SQL JOINs you can
use:

INNER JOIN: Returns all rows when there is at least one match in BOTH
tables

LEFT JOIN: Return all rows from the left table, and the matched rows from
the right table

RIGHT JOIN: Return all rows from the right table, and the matched rows
from the left table

FULL JOIN: Return all rows when there is a match in ONE of the tables

Q1. Display all data of Emp working at Loc in NOIDA.

Q2. Display all data of Emp with LOC in NEWYORK.

SOME MORE DML COMMANDS


SQL DELETE Statement- The DELETE statement is used to
delete rows in a table.

SQL DELETE Syntax


DELETE FROM table_name
WHERE some_column=some_value;

Example-DELETE FROM emp_dd where job=CLERK;

UPDATE Statement- The UPDATE statement is used to update existing records in a


table.

SQL UPDATE Syntax


UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Example-Update emp_dd set ename=Rajesh where Job=Manager;

Vous aimerez peut-être aussi