10
SQL ASSIGNMENTS AIM-Create the following tables in SQL and write the SQL Queries related to it. EmpNo Ename Job MGR Hiredat e Sal Comm DeptNo 7369 SMITH CLERK 7902 17-Dec- 80 800 20 7499 ALLEN SALESMAN 7698 20-Feb- 81 26000 2600 10 7521 WARD SALESMAN 7698 22-Feb- 81 28000 2800 50 7566 JONES MANAGER 7839 2-Apr- 81 42000 5000 40 7654 MARTIN SALESMAN 7698 28-Sep- 81 20 7698 BLAKE MANAGER 7839 1-May- 81 38000 5000 20 7782 CLARK MANAGER 7839 9-Jun- 81 40000 5000 30 7788 SCOTT ANALYST 7566 19-Apr- 81 40 7839 KING PRESIDENT 17-Nov- 81 95000 50 7844 TURNER SALESMAN 7698 8-Sep- 81 32000 3200 40 7876 ADAMS CLERK 7788 23-May- 81 18000 50 7900 AMES CLERK 7698 3-Dec- 81 21000 30 7902 FORD ANALYST 7566 3-Dec- 81 23000 50 7934 MILLER CLERK 7782 23-Jan- 82 15000 10 Dept No Dname LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON PNO EMPNO WORK_HRS PNO PNAME BUDGET S_DATE P1 7379 10 P1 Amarpal i 85000 12/1/2013

_SQL1 (2)

Embed Size (px)

DESCRIPTION

SQL queries assignment

Citation preview

Page 1: _SQL1 (2)

SQL ASSIGNMENTSAIM-Create the following tables in SQL and write the SQL Queries related to it.

EmpNo Ename Job MGR Hiredate Sal Comm DeptNo7369 SMITH CLERK 7902 17-Dec-80 800 207499 ALLEN SALESMAN 7698 20-Feb-81 26000 2600 107521 WARD SALESMAN 7698 22-Feb-81 28000 2800 507566 JONES MANAGER 7839 2-Apr-81 42000 5000 407654 MARTIN SALESMAN 7698 28-Sep-81 207698 BLAKE MANAGER 7839 1-May-81 38000 5000 207782 CLARK MANAGER 7839 9-Jun-81 40000 5000 307788 SCOTT ANALYST 7566 19-Apr-81 407839 KING PRESIDENT 17-Nov-81 95000 507844 TURNER SALESMAN 7698 8-Sep-81 32000 3200 407876 ADAMS CLERK 7788 23-May-81 18000 507900 AMES CLERK 7698 3-Dec-81 21000 307902 FORD ANALYST 7566 3-Dec-81 23000 507934 MILLER CLERK 7782 23-Jan-82 15000 10

Dept No Dname LOC10 ACCOUNTING NEW YORK20 RESEARCH DALLAS30 SALES CHICAGO40 OPERATIONS BOSTON

PNO EMPNO WORK_HRS PNO PNAME BUDGET S_DATEP1 7379 10 P1 Amarpali 85000 12/1/2013P1 7566 12 P2 Maruti 250000 1/2/2013P1 7788 8 P3 Intel 60000 21/03/2013P2 7369 8 P4 Gaur City 25000 1/7/2015

P2 7902 15 P5Godrej

City 35000 1/8/2015P3 7788 24 P6 Ford 125000 19/02/2014P4 7499 22 P7 GE 25000 13/07/2015P5 7900 14 P8 Kirloskar 75000 1/2/2014P4 7566 24P5 7654 22P6 7782 15P6 7876 36P7 7900 56P8 7876 15

Page 2: _SQL1 (2)

P9 7934 18VARIOUS 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 SYNTAX- 1. 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_nameFROM table_name;

And

SELECT * FROM table_name;

SQL QUESTIONS WITH QUERIESWHERE STAEMENT- The WHERE clause is used to filter records.

WHERE SyntaxSELECT column_name,column_nameFROM table_nameWHERE 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

Page 3: _SQL1 (2)

<= Less than or equalBETWEEN Between an inclusive rangeLIKE Search for a patternIN 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.

Page 4: _SQL1 (2)

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

DISPLAY ALL DEPTQ1. Disaplay all Dept Located in NEW YORK.

Q2. Display all DeptNo more than 20.

PATTERN MATCHINGSQL LIKE OperatorLIKE SyntaxSELECT column_name(s)FROM table_nameWHERE 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.

Page 5: _SQL1 (2)

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 FUNCTIONSThe 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 SyntaxSELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

Q1. Find the Total Salary of all Emp.

Page 6: _SQL1 (2)

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 SyntaxSELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

ORDER BY SyntaxSELECT column_name, column_nameFROM table_nameORDER BY column_name ASC|DESC, column_name ASC|DESC;

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

Page 7: _SQL1 (2)

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 QUERYQ1.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 QUERYJoins 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.

Page 8: _SQL1 (2)

Different SQL JOINsBefore 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 SyntaxDELETE FROM table_nameWHERE 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 SyntaxUPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;

Page 9: _SQL1 (2)

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