Tuesday, July 29, 2014

ssis interview quetions


• What do we mean by dataflow in SSIS?
Data flow is nothing but the flow of data from the corresponding sources to the referred destinations. In this process, the data transformations make changes to the data to make it ready for the data warehouse.
• What is a breakpoint in SSIS? How is it setup? How do you disable it?
A breakpoint is a stopping point in the code. The breakpoint can give the Developer\DBA an
opportunity to review the status of the data, variables and the overall status of the SSIS package.
10 unique conditions exist for each breakpoint.
Breakpoints are setup in BIDS. In BIDS, navigate to the control flow interface. Right click on the
object where you want to set the breakpoint and select the ‘Edit Breakpoints…’ option.
• Can you name 5 or more of the native SSIS connection managers?
1) OLEDB connection – Used to connect to any data source requiring an OLEDB connection (i.e.,
SQL Server 2000)
2) Flat file connection – Used to make a connection to a single file in the File System. Required for reading information from a File System flat file
3) ADO.Net connection – Uses the .Net Provider to make a connection to SQL Server 2005 or other
connection exposed through managed code (like C#) in a custom task
4) Analysis Services connection – Used to make a connection to an Analysis Services database or project. Required for the Analysis Services DDL Task and Analysis Services Processing Task
5) File connection – Used to reference a file or folder. The options are to either use or create a file or folder
6) Excel
• What is the use of Bulk Insert Task in SSIS?
Bulk Insert Task is used to upload large amount of data from flat files into Sql Server. It supports only OLE DB connections for destination database.
• What is Conditional Split transformation in SSIS?
This is just like IF condition which checks for the given condition and based on the condition evaluation, the output will be sent to the appropriate OUTPUT path. It has ONE input and MANY outputs. Conditional Split transformation is used to send paths to different outputs based on some conditions. For example, we can organize the transform for the students in a class who have marks greater than 40 to one path and the students who score less than 40 to another path.
• How do you eliminate quotes from being uploaded from a flat file to SQL Server? 
This can be done using TEXT QUALIFIER property. In the SSIS package on the Flat File Connection Manager Editor, enter quotes into the Text qualifier field then preview the data to ensure the quotes are not included.
• Can you explain how to setup a checkpoint file in SSIS?
The following items need to be configured on the properties tab for SSIS package:
CheckpointFileName – Specify the full path to the Checkpoint file that the package uses to save the value of package variables and log completed tasks. Rather than using a hard-coded path as shown above, it’s a good idea to use an expression that concatenates a path defined in a package variable and the package name.
CheckpointUsage – Determines if/how checkpoints are used. Choose from these options: Never(default), IfExists, or Always. Never indicates that you are not using Checkpoints. IfExists is the typical setting and implements the restart at the point of failure behavior. If a Checkpoint file is found it is used to restore package variable values and restart at the point of failure. If a Checkpoint file is not found the package starts execution with the first task. The Always choice raises an error if the Checkpoint file does not exist.
SaveCheckpoints – Choose from these options: True or False (default). You must select True to implement the Checkpoint behavior.
• What are the different values you can set for CheckpointUsage property ?
There are three values, which describe how a checkpoint file is used during package execution:
1) Never: The package will not use a checkpoint file and therefore will never restart.
2) If Exists: If a checkpoint file exists in the place you specified for the CheckpointFilename property, then it will be used, and the package will restart according to the checkpoints written.
3) Always: The package will always use a checkpoint file to restart, and if one does not exist, the package will fail.
What is the ONLY Property you need to set on TASKS in order to configure CHECKPOINTS to RESTART package from failure?
The one property you have to set on the task is FailPackageOnFailure. This must be set for each task or container that you want to be the point for a checkpoint and restart. If you do not set this property to true and the task fails, no file will be written, and the next time you invoke the package, it will start from the beginning again.
• Where can we set the CHECKPOINTS, in DataFlow or ControlFlow ?
Checkpoints only happen at the Control Flow; it is not possible to checkpoint transformations or restart inside a Data Flow. The Data Flow Task can be a checkpoint, but it is treated as any other task.
• Can you explain different options for dynamic configurations in SSIS?
1) XML file
2) custom variables
3) Database per environment with the variables
4) Use a centralized database with all variables
• What is the use of Percentage Sampling transformation in SSIS?
Percentage Sampling transformation is generally used for data mining. This transformation builds a random sample of set of output rows by choosing specified percentage of input rows. For example if the input has 1000 rows and if I specify 10 as percentage sample then the transformation returns 10% of the RANDOM records from the input data.
• What is the use of Term Extraction transformation in SSIS?
Term Extraction transformation is used to extract nouns or noun phrases or both noun and noun phrases only from English text. It extracts terms from text in a transformation input column and then writes the terms to a transformation output column. It can be also used to find out the content of a dataset.
• What is Data Viewer and what are the different types of Data Viewers in SSIS?
A Data Viewer allows viewing data at a point of time at runtime. If data viewer is placed before and after the Aggregate transform, we can see data flowing to the transformation at the runtime and how it looks like after the transformation occurred. The different types of data viewers are:
1. Grid
2. Histogram
3. Scatter Plot
4. Column Chart.
• What is Ignore Failure option in SSIS?
In Ignore Failure option, the error will be ignored and the data row will be directed to continue on the next transformation. Let’s say you have some JUNK data(wrong type of data or JUNK data) flowing from source, then using this option in SSIS we can REDIRECT the junk data records to another transformation instead of FAILING the package. This helps to MOVE only valid data to destination and JUNK can be captured into separate file.
• Which are the different types of Control Flow components in SSIS?
The different types of Control Flow components are: Data Flow Tasks, SQL Server Tasks, Data Preparation Tasks, Work flow Tasks, Scripting Tasks, Analysis Services Tasks, Maintenance Tasks, Containers.
• What are containers? What are the different types of containers in SSIS?
Containers are objects that provide structures to packages and extra functionality to tasks. There are four types of containers in SSIS, they are: Foreach Loop Container, For Loop Container, Sequence Container and Task Host Container
• What are the different types of data sources available in SSIS?
There are 7 types of data sources provided by SSIS: a.) Data Reader source b.) Excel source c.) Flat file source d.) OLEDB source e.) Raw file source f.) XML source g.) Script component
• What is SSIS Designer?
It is a graphical tool for creating packages. It has 4 tabs: Control Flow, Data Flow, Event Handlers and Package Explorer.
• What is Control Flow tab?
It is the tab in SSIS designer where various Tasks can be arranged and configured. This is the tab where we provide and control the program flow of the project.
• What is Data Flow tab?
This is the tab where we do all the work related to ETL job. It is the tab in SSIS Designer where we can extract data from sources, transform the data and then load them into destinations.
• What is the function of control flow tab in SSIS?
On the control flow tab, the tasks including dataflow task, containers and precedence constraints that connect containers and tasks can be arranged and configured.
• What is the function of Event handlers tab in SSIS?
On the Event handlers tab, workflows can be configured to respond to package events.
For example, we can configure Work Flow when ANY task Failes or Stops or Starts ..
• What is the function of Package explorer tab in SSIS?
This tab provides an explorer view of the package. You can see what is happening in the package. The Package is a container at the top of the hierarchy.
• What is Solution Explorer?
It is a place in SSIS Designer where all the projects, Data Sources, Data Source Views and other miscellaneous files can be viewed and accessed for modification.
• How do we convert data type in SSIS?
The Data Conversion Transformation in SSIS converts the data type of an input column to a different data type.
• How are variables useful in ssis package?
Variables can provide communication among objects in the package. Variables can provide communication between parent and child packages. Variables can also be used in expressions and scripts. This helps in providing dynamic values to tasks.
• Explain Aggregate Transformation in SSIS?
It aggregates data, similar you do in applying TSQL functions like Group By, Min, Max, Avg, and Count. For example you get total quantity and Total line item for each product in Aggregate Transformation Editor. First you determine input columns, then output column name in Output Alias table in datagrid, and also operations for each Output Alias in Operation columns of the same datagrid. Some of operation functions listed below :
• Group By
• Average
• Count
• Count Distinct : count distinct and non null column value
• Min, Max, Sum
In Advanced tab, you can do some optimization here, such as setting up Key Scale option (low, medium, high), Count Distinct scale option (low, medium, high), Auto Extend factor and Warn On Division By Zero. If you check Warn On Division By Zero, the component will give warning instead of error. Key Scale option will optimize transformation cache to certain number of key threshold. If you set it low, optimization will target to 500,000 keys written to cache, medium can handle up to 5 million keys, and high can handle up to 25 million keys, or you can specify particular number of keys here. Default value is unspecified. Similar to number of keys for Count Distinct scale option. It is used to optimize number of distinct value written to memory, default value is unspecified. Auto Extend Factor is used when you want some portion of memory is used for this component. Default value is 25% of memory.
• Explain Audit Transformation ?
It allows you to add auditing information as required in auditing world specified by HIPPA and Sarbanes-Oxley (SOX). Auditing options that you can add to transformed data through this transformation are :
1. Execution of Instance GUID : ID of execution instance of the package
2. PackageID : ID of the package
3. PackageName
4. VersionID : GUID version of the package
5. Execution StartTime
6. MachineName
7. UserName
8. TaskName
9. TaskID : uniqueidentifier type of the data flow task that contains audit transformation.
• Explain Character Map Transformation?
It transforms some character. It gives options whether output result will override the existing column or add to new column. If you define it as new column, specify new column name. Operations available here are:
1. Uppercase
2. Lowercase
3. Byte reversal : such as from 0×1234 to 0×4321
4. Full width
5. Half width
6. Hiragana/katakana/traditional Chinese/simplified Chinese
7. Linguistic casing
• Explain Conditional split Transformation ?
It functions as if…then…else construct. It enables send input data to a satisfied conditional branch. For example you want to split product quantity between less than 500 and greater or equal to 500. You can give the conditional a name that easily identifies its purpose. Else section will be covered in Default Output Column name.
After you configure the component, it connect to subsequent transformation/destination, when connected, it pops up dialog box to let you choose which conditional options will apply to the destination transformation/destination.
• Explain Copy column Transformation?
This component simply copies a column to another new column. Just like ALIAS Column in T-Sql.
• Explain Data conversion Transformation?
This component does conversion data type, similar to TSQL function CAST or CONVERT. If you wish to convery the data from one type to another then this is the best bet. But please make sure that you have COMPATABLE data in the column.
• Explain Data Mining query Transformation?
This component does prediction on the data or fills gap on it. Some good scenarios uses this component is:
1. Take some input columns as number of children, domestic income, and marital income to predict whether someone owns a house or not.
2. Take prediction what a customer would buy based analysis buying pattern on their shopping cart.
3. Filling blank data or default values when customer doesn’t fill some items in the questionnaire.
• Explain Derived column Transformation?
Derived column creates new column or put manipulation of several columns into new column. You can directly copy existing or create a new column using more than one column also.
• Explain Merge Transformation?
Merge transformation merges two paths into single path. It is useful when you want to break out data into path that handles errors after the errors are handled, the data are merge back into downstream or you want to merge 2 data sources. It is similar with Union All transformation, but Merge has some restrictions :
1. Data should be in sorted order
2. Data type , data length and other meta data attribute must be similar before merged.
• Explain Merge Join Transformation?
Merge Join transformation will merge output from 2 inputs and doing INNER or OUTER join on the data. But if you the data come from 1 OLEDB data source, it is better you join through SQL query rather than using Merge Join transformation. Merge Join is intended to join 2 different data source.
• Explain Multicast Transformation?
This transformation sends output to multiple output paths with no conditional as Conditional Split does. Takes ONE Input and makes the COPY of data and passes the same data through many outputs. In simple Give one input and take many outputs of the same data.
• Explain Percentage and row sampling Transformations?
This transformation will take data from source and randomly sampling data. It gives you 2 outputs. First is selected data and second one is unselected data. It is used in situation where you train data mining model. These two are used to take the SAMPLE of data from the input data.
• Explain Sort Transformation?
This component will sort data, similar in TSQL command ORDER BY. Some transformations need sorted data.
• Explain Union all Transformation?
It works in opposite way to Merge transformation. It can take output from more than 2 input paths and combines into single output path.
• What r the possible locations to save SSIS package?
You can save a package wherever you want.
SQL Server
Package Store
File System
• What is a package?
A discrete executable unit of work composed of a collection of control flow and other objects, including data sources, transformations, process sequence, and rules, errors and event handling, and data destinations.
• What is a workflow in SSIS?
A workflow is a set of instructions on how to execute tasks.
(It is a set of instructions on how to execute tasks such as sessions, emails and shell commands. a workflow is created form work flow mgr.
• What is the diff between control flow Items and data flow Items?
The control flow is the highest level control process. It allows you to manage the run-time process activities of data flow and other processes within a package.
When we want to extract, transform and load data within a package. You add an SSIS dataflow task to the package control flow.
• What are the main component of SSIS(project-architecture)?
SSIS archItecture has 4 main components
1.ssis service
2.ssis runtime engine & runtime executables
3.ssis dataflow engine & dataflow components
4.ssis clients
• Different components in SSIS package?
Control flow
Data flow
Event handler
Package explorer
• What are Connection Managers?
It is a bridge b/w package object and physical data. It provides logical representation of a connection at design time the properties of the connection mgr describes the physical connection that integration services creates when the package is run.
• What is environment variable in SSIS?
An environment variable configuration sets a package property equal to the value in an environment variable.
Environmental configurations are useful for configuring properties that are dependent on the computer that is executing the package.
• How to provide securIty to packages?
We can provide security in two ways
1. Package encryption
2. Password protection.
• What are Precedence constraints?
Constraints that link executable, container, and tasks wIthin the package control flow and specify condItion that determine the sequence and condItions for determine whether executable run.
• What is Design time Deployment in SSIS ?
When you run a package from with in BIDS,it is built and temporarily deployed to the folder. By default the package will be deployed to the BIN folder in the Package’s Project folder and you can configure for custom folder for deployment. When the Package’s execution is completed and stopped in BIDS,the deployed package will be deleted and this is called as Design Time Deployment.
TOP 10 SSIS INTERVIEW QUETIONS:

1. What are the different types of Data flow components in SSIS?
There are 3 data flow components in SSIS.
1. Sources
2. Transformations
3. Destinations
2. Explain Audit Transformation ?
It allows you to add auditing information as required in auditing world specified by HIPPA and Sarbanes-Oxley (SOX). Auditing options that you can add to transformed data through this transformation are :
1. Execution of Instance GUID : ID of execution instance of the package
2. PackageID : ID of the package
3. PackageName
4.VersionID : GUID version of the package
5. Execution StartTime
6.MachineName
7.UserName
8.TaskName
9.TaskID : uniqueidentifier type of the data flow task that contains audit transformation
3. Explain Copy column Transformation?
This component simply copies a column to another new column. Just like ALIAS Column in T-Sql.
4. Explain Derived column Transformation?
Derived column creates new column or put manipulation of several columns into new column. You can directly copy existing or create a new column using more than one column also.
5.Explain Multicast Transformation?
This transformation sends output to multiple output paths with no conditional as Conditional Split does. Takes ONE Input and makes the COPY of data and passes the same data through many outputs. In simple Give one input and take many outputs of the same data.
6.What is a Task?
A task is very much like a method of any programming language which represents or carries out an individual unit of work. There are broadly two categories of tasks in SSIS, Control Flow tasks and Database Maintenance tasks. All Control Flow tasks are operational in nature except Data Flow tasks. Although there are around 30 control flow tasks which you can use in your package you can also develop your own custom tasks with your choice of .NET programming language

7.What is a workflow in SSIS ?
Workflow is a set of instructions on to specify the Program Executor on how to execute tasks and containers within SSIS Packages
8.What is the Control Flow?
 When you start working with SSIS, you first create a package which is nothing but a collection of tasks or package components.  The control flow allows you to order the workflow, so you can ensure tasks/components get executed in the appropriate order
9.What is a Transformation?
 A transformation simply means bringing in the data in a desired format. For example you are pulling data from the source and want to ensure only distinct records are written to the destination, so duplicates are  removed.  Anther example is if you have master/reference data and want to pull only related data from the source and hence you need some sort of lookup. There are around 30 transformation tasks available and this can be extended further with custom built tasks if needed.
10.How many difference source and destinations have you used?
It is very common to get all kinds of sources so the more the person worked with the better for you. Common ones are SQL Server, CSV/TXT, Flat Files, Excel, Access, Oracle, MySQL but also Salesforce, web data scrapping.







What is SQL Server Integration Services (SSIS)?

  • SQL Server Integration Services (SSIS) is component of SQL Server 2005 and later versions. SSIS is an enterprise scale ETL (Extraction, Transformation and Load) tool which allows you to develop data integration and workflow solutions. Apart from data integration, SSIS can be used to define workflows to automate updating multi-dimensional cubes and automating maintenance tasks for SQL Server databases.
  • This is a platform for Integration and Workflow applications. It is known for a fast and flexible OLTP and OLAP extensions used for data extraction, transformation, and loading (ETL). The tool may also be used to automate maintenance of SQL Server databases and multidimensional data sets.

How does SSIS differ from DTS?

  • SSIS is a successor to DTS (Data Transformation Services) and has been completely re-written from scratch to overcome the limitations of DTS which was available in SQL Server 2000 and earlier versions. A significant improvement is the segregation of the control/work flow from the data flow and the ability to use a buffer/memory oriented architecture for data flows and transformations which improve performance.
Data Transformation Services
SQL Server Integration Services
Limited Error Handling
Complex and powerful Error Handling
Message Boxes in ActiveX Scripts
Message Boxes in .NET Scripting 
No Deployment Wizard
Interactive Deployment Wizard
Limited Set of Transformation
Good number of Transformations
NO BI functionality
Complete BI Integration


What is the Control Flow?

  • When you start working with SSIS, you first create a package which is nothing but a collection of tasks or package components.  The control flow allows you to order the workflow, so you can ensure tasks/components get executed in the appropriate order.


What is the Data Flow Engine?

  • The Data Flow Engine, also called the SSIS pipeline engine, is responsible for managing the flow of data from the source to the destination and performing transformations (lookups, data cleansing etc.).  Data flow uses memory oriented architecture, called buffers, during the data flow and transformations which allows it to execute extremely fast. This means the SSIS pipeline engine pulls data from the source, stores it in buffers (in-memory), does the requested transformations in the buffers and writes to the destination. The benefit is that it provides the fastest transformation as it happens in memory and we don't need to stage the data for transformations in most cases.


What is a Transformation?

  •  A transformation simply means bringing in the data in a desired format. For example you are pulling data from the source and want to ensure only distinct records are written to the destination, so duplicates are  removed.  Anther example is if you have master/reference data and want to pull only related data from the source and hence you need some sort of lookup. There are around 30 transformation tasks available and this can be extended further with custom built tasks if needed.


What is a Task?

  • A task is very much like a method of any programming language which represents or carries out an individual unit of work. There are broadly two categories of tasks in SSIS, Control Flow tasks and Database Maintenance tasks. All Control Flow tasks are operational in nature except Data Flow tasks. Although there are around 30 control flow tasks which you can use in your package you can also develop your own custom tasks with your choice of .NET programming language.

What is a Precedence Constraint and what types of Precedence Constraint are there?

  • SSIS allows you to place as many as tasks you want to be placed in control flow. You can connect all these tasks using connectors called Precedence Constraints. Precedence Constraints allow you to define the logical sequence of tasks in the order they should be executed. You can also specify a condition to be evaluated before the next task in the flow is executed.
  • These are the types of precedence constraints and the condition could be either a constraint, an expression or both 
    • Success (next task will be executed only when the last task completed successfully) or
    • Failure (next task will be executed only when the last task failed) or
    • Complete (next task will be executed no matter the last task was completed or failed).

What is a container and how many types of containers are there?

  • A container is a logical grouping of tasks which allows you to manage the scope of the tasks together.
  • These are the types of containers in SSIS:
    • Sequence Container - Used for grouping logically related tasks together
    • For Loop Container - Used when you want to have repeating flow in package
    • For Each Loop Container - Used for enumerating each object in a collection; for example a record set or a list of files.
  • Apart from the above mentioned containers, there is one more container called the Task Host Container which is not visible from the IDE, but every task is contained in it (the default container for all the tasks).

What are variables and what is variable scope?

  • A variable is used to store values. There are basically two types of variables, System Variable (like ErrorCode, ErrorDescription, PackageName etc) whose values you can use but cannot change and User Variable which you create, assign values and read as needed. A variable can hold a value of the data type you have chosen when you defined the variable.
  • Variables can have a different scope depending on where it was defined. For example you can have package level variables which are accessible to all the tasks in the package and there could also be container level variables which are accessible only to those tasks that are within the container.
How many difference source and destinations have you used?
  •  It is very common to get all kinds of sources so the more the person worked with the better for you. Common ones are SQL Server, CSV/TXT, Flat Files, Excel, Access, Oracle, MySQL but also Salesforce, web data scrapping.

What configuration options have you used?
  •  This is an important one. Configuration should always be dynamic and usually is done using XML and/or Environment Variable and SQL Table with all configurations.

How do you apply business rules in SSIS (Transformations….Specific calculations but also cleansing)?
  •  Some people use SSIS only to extract data and then go with stored procedures only….they are usually missing the point of the power of SSIS. Which allows to create "a flow" and on each step applies certain rules this greatly simplifies the ETL process and simplicity is very good.

 How to quickly load data into sql server table?
  •  Fast Load option. This option is not set by default so most developers know this answer as otherwise the load is very slow.

Give example of handling data quality issues?
  •  Data Quality is almost always a problem and SSIS handles it very well. Examples include importing customers from different sources where customer name can be duplicates. For instance you can have as company name: SQL Server Business Intelligence but also SQL Server BI or SQL Server BI LTD or SQL Server BI Limited or intelligence (with one l). There are different ways to handle it. Robust and time consuming is to create a table with or possible scenarios and update it after each update. You can also use fuzzy grouping which is usually easy to implement and will make usually very good decisions but it is not 100% accurate so this approach has to be justified. Other typical quality issues are nulls (missing values), outliers (dates like 2999 or types like 50000 instead of 5000 especially important if someone is adjusting the value to get bigger bonus), incorrect addresses and these are either corrected during ETL, ignored, re-directed for further manual updates or it fails the packages which for big processes is usually not practised.

 When to use Stored Procedures?
  •  This was one of the requested question in comment (at the bottom of the page). This one is very important but also tricky. ALL SSIS developers have SQL Server background and that is sometime not very good if they use SQL not SSIS approach.
Let's start with when you typically use SPs. This is for preparing tables (truncate), audit tasks (usually part of SSIS framework), getting configuration values for loops and a few other general tasks.
During ETL extract you usually type simple SQL because it comes from other sources and usually over complication is not a good choice (make it dynamic) because any changes usually affect the package which has to be updated as well.
During Transformation phase (business rules, cleaning, core work) you should use Transformation tasks not Stored procedures! There are loads of tasks that make the package much easier to develop but also a very important reason is readability which is very important for other people who need to change the package and obviously it reduces risks of making errors. Performance is usually very good with SSIS as it is memory/flow based approach. So when to use Stored Procedures for transformations? If you don't have strong SSIS developers or you have performance reasons to do it. In some cases SPs can be much faster (usually it only applies to very large datasets). Most important is have reasons which approach is better for the situation.

What is your approach for ETL with data warehouses (how many packages you developer during typical load etc.)?
  • This is rather generic question. A typical approach (for me) when building ETL is to. Have a package to extract data per source with extract specific transformations (lookups, business rules, cleaning) and loads data into staging table. Then a package do a simple merge from staging to data warehouse (Stored Procedure) or a package that takes data from staging and performs extra work before loading to data warehouse. I prefer the first one and due to this approach I occasionally consider having extract stage (as well as stage phase) which gives me more flexibility with transformation (per source) and makes it simpler to follow (not everything in one go). So to summarize you usually have package per source and one package per data warehouse table destination. There are might be other approach valid as well so ask for reasons.

Tuesday, July 15, 2014

SQL QUARIES

SQL QUARIES:



1.  Display all the information of the EMP table?
select * from emp

2.  Display unique Jobs from EMP table?
select distinct(job) from emp

3.  List the emps in the asc order of their Salaries?
select * from emp order by sal asc

4.  List the details of the emps in asc order of the Dptnos and desc of Jobs?
select * from emp order by deptno asc,job desc

5.  Display all the unique job groups in the descending order?
select distinct(job) from emp order by job desc

6.  Display all the details of all Mgrs?
select * from emp where job='manager'

7.  List the emps who joined before 1981.
select * from emp where hiredate >'1981'

8.  List the Empno, Ename, Sal, Daily sal of all emps in the asc order of Annsal.
select empno,ename,sal,sal/30,sal*12 as annsal from emp order by annsal asc

9.  Display the Empno, Ename, job, Hiredate, Exp of all Mgrs
select empno,ename,job,hiredate,datediff(yy,hiredate,getdate())as exps from emp
where job='manager'

10. List the Empno, Ename, Sal, Exp of all emps working for Mgr 7369.
select empno,ename,sal,datediff(yy,hiredate,getdate())as exps from emp
 WHERE mgr='7369'

11. Display all the details of the emps whose Comm. Is more than their Sal.
select * from emp where comm >sal

12. List the emps in the asc order of Designations of those joined after the second half of 1981.
SELECT * FROM EMP WHERE DATEPART(Q,HIREDATE)=2 AND DATEPART('YYYY',HIREDATE)='1981'

13. List the emps along with their Exp and Daily Sal is more than Rs.100.
select *,sal/30 as dailysal,datediff(yy,hiredate,getdate()) as exp from emp
where sal/30 >100

14. List the emps who are either ‘CLERK’ or ‘ANALYST’ in the Desc order.
select * from emp where job in('clerk','analyst') order by job desc

15. List the emps who joined on 1-MAY-81,3-DEC-81,17-DEC-81,19-JAN-80 in asc order of seniority.
select * from emp where hiredate in('05-01-81','12-03-81','12-17-81','1-19-80')
order by hiredate asc

16. List the emp who are working for the Deptno 10 or20.
select * from emp where deptno in(10,20)

17. List the emps who are joined in the year 81.
select * from emp where datepart(yyyy,hiredate)= '1981'

18. List the emps who are joined in the month of Aug 1980.
 select * from emp where datepart(yyyy,hiredate)= '1980' and datepart(mm,hiredate)='08' 

19. List the emps Who Annual sal ranging from 22000 and 45000.
select *,sal*12 from emp where sal*12 between 22000 and 45000

20. List the Enames those are having five characters in their Names.
select * from emp where len(ename)=5

21. List the Enames those are starting with ‘S’ and with five characters.
select * from emp where ename like 's%'

22. List the emps those are having four chars and third character must be ‘r’.
select * from emp where len(ename)=4 and ename like'__r%'

23. List the Five character names starting with ‘S’ and ending with ‘H’.
select * from emp where ename like 's___h%'

24. List the emps who joined in January.
select * from emp where datepart(mm,hiredate)='01'

25. List the emps who joined in the month of which second character is ‘a’.
select * from emp where hiredate like '_a%'

26. List the emps whose Sal is four digit number ending with Zero.
select * from emp where sal like '___0%'

27. List the emps whose names having a character set ‘ll’ together.
select * from emp where ename like '%LL%'

28. List the emps those who joined in 80’s.
select * from emp where datepart(yyyy,hiredate)='1980'

29. List the emps who does not belong to Deptno 20.
select * from emp where deptno <> 20

30. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.
select * from emp where job not in ('manager' ,'president') order by sal asc
           
31. List all the emps who joined before or after 1981.
select * from emp where datepart(yyyy,hiredate) <> '1981'

32. List the emps whose Empno not starting with digit78.
select * from emp where empno not like'78%'

33. List the emps who are working under ‘MGR’.
select * from emp where mgr in(select empno from emp where job='manager')

34. List the emps who joined in any year but not belongs to the month of March.
select * from emp where datepart(mm,hiredate)<>'03'

35. List all the Clerks of Deptno 20.
select * from emp where deptno=20 and job='clerk'
           
36. List the emps of Deptno 30 or 10 joined in the year 1981.
select * from emp where deptno in(10,30)and datepart(yyyy,hiredate) = 1981
           
37. Display the details of SMITH.
select * from emp where ename='smith'

38. Display the location of  SMITH.
select * from emp e , dept d  where ename='smith' and e.deptno=d.deptno  --or
select * from emp e inner join dept d on e.deptno=d.deptno where ename='smith'

39. List the total information of EMP table along with DNAME
and Loc of all the emps Working Under ‘ACCOUNTING’ & ‘RESEARCH’ in the asc Deptno.
select e.*,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno and
 d.dname in('accounting','research') order by deptno asc       --or
SELECT * FROM EMP E,DEPT D WHERE D.DNAME IN('ACCOUNTING','RESEARCH')
AND E.DEPTNO=D.DEPTNO ORDER BY E.DEPTNO ASC;

40. List  the  Empno,  Ename,  Sal,  Dname  of  all  the  ‘MGRS’  and  ‘ANALYST’ working
in New York, Dallas with an exp more than 7 years without receiving the Comm asc order of Loc.
select e.empno,e.ename,e.sal,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno
 where job in('manager','analyst')
 and d.loc in('newyork','dallas')
 and datediff(yy,hiredate,getdate())>7
 and comm is null order by d.loc asc             

41. Display the Empno, Ename, Sal, Dname, Loc, Deptno, Job of all emps working at
ChICAGO or working for ACCOUNTING dept with Ann Sal>28000, but the Sal should
not be=3000 or 2800 who doesn’t belongs to the Mgr and whose no is having a digit ‘7’ or ‘8’ in
3rd  position in the asc order of Deptno and desc order of job.
select e.*,d.* from emp e,dept d where (d.loc ='chicago' or d.dname ='ACCOUNTING')
and e.deptno=d.deptno
and e.sal*12>28000 and (e.sal <>2800 or e.sal <>3000)
and e.JOB NOT IN ('MANAGER')
and (e.empno like'__7_' and e.empno like'__8_')
order by e.deptno asc,e.job desc

42. Display the total information of the emps along with Grades in the asc order.
SELECT *FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.losal AND S.hisal ORDER BY GRADE ASC;

43. List all the Grade2 and Grade 3 emps.
SELECT *FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(2,3)

44. Display all Grade 4,5 Analyst and Mgr.
SELECT *FROM EMP E,SALGRADE S,DEPT D WHERE E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(4,5)
AND  JOB IN('MANAGER','Analyst')

45. List the Empno, Ename, Sal, Dname, Grade, Exp, and Ann Sal of emps working for Dept10 or20.
SELECT E.*,D.DNAME,S.GRADE,E.SAL*12 AS ANNSAL,DATEDIFF(YYYY,HIREDATE,GETDATE()) AS YEARS
FROM EMP E,DEPT D,SALGRADE S where e.deptno in (10,20) and
E.deptno = D.deptno and E.sal between S.losal and S.hisal ;

46. List all the information of emp with Loc and the Grade of all the emps belong to the Grade
range from 2 to 4 working at the Dept those are not starting with char set ‘OP’ and not
ending with ‘S’ with the designation having a char ‘a’ any where joined in
the year 1981 but not in the month of Mar or Sep and Sal not end with‘00’ in the asc order of Grades?
SELECT *FROM EMP E,SALGRADE S,DEPT D WHERE E.DEPTNO=D.DEPTNO AND
E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(2,3,4) AND
(D.DNAME NOT LIKE'OP%' AND D.DNAME NOT LIKE'%S') AND
 empno in (select empno from emp where job like '%A%')and
sal not like '' and
 datepart(yyyy,hiredate)= '1981' AND
 datepart(mm,hiredate) NOT IN ('03','09') ORDER BY S.GRADE ASC

47. List the details of the emps whose Salaries more than the employee BLAKE.
SELECT * FROM EMP WHERE SAL >(SELECT SAL FROM EMP WHERE ENAME='BLAKE')

48. List the emps whose Jobs are same as ALLEN.
select * from emp where job=(select job from emp where ename='allen')

49. List the emps who are senior to King.
SELECT * FROM EMP WHERE HIREDATE <(SELECT HIREDATE FROM EMP WHERE ENAME='KING')

50. List the Emps of Deptno 20 whose Jobs are same as Deptno10.
SELECT * FROM EMP WHERE DEPTNO=20 AND JOB IN(SELECT JOB FROM EMP WHERE DEPTNO=10)

51. List the Emps whose Sal is same as FORD or SMITH in desc order of Sal.
SELECT * FROM EMP WHERE SAL in (select sal from emp where ename='ford' or ename ='smith')
 ORDER BY SAL DESC;

52. List the emps Whose Jobs are same as MILLER or Sal is more than ALLEN.
SELECT * FROM EMP WHERE  job=(select JOB from emp where ename='miller' )or
sal>(SELECT SAL FROM EMP WHERE ENAME='allen')

53. List the Emps whose Sal is > the total remuneration of the SALESMAN.
SELECT MAX(SAL) FROM EMP WHERE SAL>
(SELECT SUM((SAL+ISNULL(COMM,0))) FROM EMP WHERE JOB='SALESMAN')

54. List the emps who are senior to BLAKE working at CHICAGO & BOSTON.
SELECT * FROM EMP ,DEPT  WHERE EMP.HIREDATE <
(SELECT EMP.HIREDATE FROM EMP WHERE EMP.ENAME ='BLAKE') AND
DEPT.LOC IN('CHICAGO','BOSTON') AND EMP.DEPTNO=DEPT.DEPTNO

55. List the Emps of Grade 3,4 belongs to the dept ACCOUNTING and RESEARCH whose
Sal is more than ALLEN and exp more than SMITH in the asc order of EXP.
SELECT * FROM EMP E WHERE E.DEPTNO IN(SELECT D.DEPTNO FROM DEPT D WHERE
D.DNAME IN('ACCOUNTING','RESEARCH'))AND
E.SAL >(SELECT SAL FROM EMP WHERE ENAME='ALLEN') AND
E.HIREDATE<(SELECT HIREDATE FROM EMP WHERE ENAME='SMITH') AND
E.EMPNO IN(SELECT E.EMPNO FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND
S.HISAL AND s.grade in (3,4) ) order by hiredate desc

56. List the emps whose jobs same as SMITH or ALLEN.
SELECT * FROM EMP WHERE JOB IN(SELECT JOB FROM EMP WHERE ENAME ='SMITH'OR ENAME='ALLEN')

57. Any jobs of deptno 10 those that are not found in deptno 20.
SELECT * FROM EMP WHERE DEPTNO=10 AND JOB NOT IN(SELECT JOB FROM EMP WHERE DEPTNO=20)

58. Find the highest sal of EMP table.
SELECT MAX(SAL) FROM EMP

59. Find details of highest paid employee.
SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP)

60. Find the highest paid employee of sales department.
SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP WHERE JOB='SALESMAN')

61. List the most recently hired emp of grade3 belongs to  location CHICAGO.
select * from emp e,dept d where d.loc='CHICAGO' and hiredate in
(select max(hiredate)  from emp e,salgrade s
where sal between losal and hisal and grade=3) 

62. List the employees who are senior to most recently hired employee working under king.
  select * from emp where hiredate <(select max(hiredate) from emp where mgr in
  (select empno from emp where ename='king'))

  select top(1) ename from emp where hiredate<(select
  hiredate from emp where ename='king')order by
 hiredate desc

63. List the details of the employee belongs to newyork with grade 3 to 5 except
'PRESIDENT’ whose sal> the highest paid employee of Chicago in a group where
there is manager and salesman not working under king?
select * from emp where deptno in (select deptno from dept where dept.loc ='NEW YORK') and empno in
 (select empno from emp e,salgrade s where e.sal between s.losal and s.hisal and s.grade in (3,4,5) ) and job !=
 'PRESIDENT' and sal >(select max(sal) from emp where deptno in (select deptno from dept where dept.loc = 'CHICAGO')
  and job in ('MANAGER','SALESMAN') and mgr not in (select empno from emp where ename='KING'))

64. List the details of the senior employee belongs to 1981.
SELECT * FROM EMP WHERE HIREDATE IN
(SELECT MIN(HIREDATE) FROM EMP WHERE DATEPART(YYYY,HIREDATE) = '1981')

--65. List the employees who joined in 1981 with the job same as the most senior person of the year 1981.
SELECT * FROM EMP WHERE JOB IN(SELECT JOB FROM EMP WHERE HIREDATE IN
(SELECT MIN(HIREDATE) FROM EMP WHERE DATEPART(YYYY,HIREDATE) = '1981'))

66. List the most senior empl working under the king and grade is more than 3.
SELECT * FROM EMP where hiredate in (select hiredate from emp where empno in
(select empno from emp e ,salgrade s where e.sal between s.losal and s.hisal and s.grade in (4,5)))
and mgr in (select empno from emp where ename = 'KING');

67. Find the total sal given to the MGR.
select sum(sal) from emp where job='manager'

68. Find the total annual sal to distribute job wise in the year 81.
select job,sum(sal*12)as tsal from emp where datepart(yyyy,hiredate)='1981' group by job

69. Display total sal employee belonging to grade 3.
select sum(sal) from emp where empno in(select empno from emp e,salgrade s where e.sal
between s.losal and s.hisal and s.grade=3)

70. Display the average salaries of all the clerks.
select avg(sal) from emp where job='clerk'

71. List the employeein dept 20 whose sal is >the average sal 0f dept 10 emps.
select * from emp where deptno=20 and sal>(select avg(sal) from emp where deptno=10)
           
72. Display the number of employee  for each job group deptno wise.
select count(*),job,deptno from emp group by job,deptno

73. List the manager no and the number of employees working for those mgrs in the ascending Mgrno.
select e.mgr,count(*) from emp e,emp m where e.mgr=m.empno group by e.mgr order by e.mgr    asc;

74. List the department,details where at least two emps are working
 SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO HAVING COUNT(*)>2;

75. Display the Grade, Number of emps, and max sal of each grade.
select s.grade,count(*) as cnt,max(sal) as sals from emp e,salgrade s where
  e.sal between s.losal and s.hisal group by s.grade

76. Display dname, grade, No. of emps where at least two emps are clerks.
select d.dname,s.grade,count(*) from emp e,dept d,salgrade s where
  e.deptno=d.deptno and e.job='clerk' and e.sal between s.losal and s.hisal
  group by d.dname,s.grade having count(*)>=2

77. List the details of the department where maximum number of emps are working.
select * from dept where deptno in (select deptno from emp group by deptno
having count(*) in (select max(count(*)) from emp group by deptno) )

78. Display the emps whose manager name is jones.
 select * from emp where mgr in
(select empno from emp where ename = 'JONES')

79. List the employees whose salary is more than 3000 after giving 20% increment.
select * from emp where (sal*0.2) >3000

80. List the emps with dept names.
select e.*,d.dname from emp e,dept d where e.deptno=d.deptno

81. List the emps who are not working in sales dept.
select * from EMP where DEPTNO NOT in(select DEPTNO from dept WHERE dNAME='SALES')
           
82. List the emps name ,dept, sal and comm. For those whose salary is between 2000
and 5000 while loc is Chicago.
select e.ename,d.deptno,e.sal,e.comm from emp e,dept d where e.deptno=d.deptno
and d.loc='chicago' and e.sal between 2000 and 5000

83. List the emps whose sal is greater than his managers salary
 select * from emp w,emp m where w.mgr = m.empno and w.sal > m.sal

84. List the grade, EMP name for the deptno 10 or deptno 30 but sal grade is not
4 while they joined the company before ’31-dec-82’.
select s.grade,e.ename,e.hiredate from emp e,salgrade s where e.sal between s.losal and s.hisal
and e.deptno in(10,30) and s.grade not in(4) and hiredate<'12/31/82'

85. List the name ,job, dname, location for those who are working as MGRS.
select e.ename,e.job,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno and empno in
(select empno from emp where job='manager')

86. List the emps whose mgr name is jones and also list their manager name
select w.*,m.ename from emp w,emp m where w.mgr=m.empno and m.ename='jones'

87. List the name and salary of ford if his salary is equal to hisal of his grade.
select e.*,s.* from emp e,salgrade s where ename='jones' and e.sal between s.losal and s.hisal
and e.sal =s.hsial

88. Lit the name, job, dname ,sal, grade dept wise
select e.ename,e.job,d.dname,e.sal,s.grade from emp e,dept d,salgrade s where e.deptno=d.deptno
and e.sal between s.losal and s.hisal order by e.deptno

89. List the emp name, job, sal, grade and dname except clerks and sort on the basis of highest sal
select e.ename,e.job,d.dname,e.sal,s.grade from emp e,dept d,salgrade s where e.deptno=d.deptno
and e.sal between s.losal and s.hisal and e.job not in('clerk') order by e.sal desc

90. List the emps name, job  who are with out manager.
select e.ename,e.job from emp e where mgr is null

91. List the names of the emps who are getting the highest sal dept wise.
select * from emp where sal in(select max(sal) from emp group by deptno)

92. List the emps whose sal is equal to the average of max and minimum
select avg(sal) from emp where sal=(select max(sal)+min(sal)/2 from emp)
           
93. List the no. of emps in each department where the no. is more than 3.
select count(*),deptno from emp group by deptno having count(*)>3

94. List the names of depts. Where atleast 3 are working in that department.
select d.dname,count(*) from emp e ,dept d where e.deptno = d.deptno
group by d.dname having count(*) >= 3

95. List the managers whose sal is more than his employess avg salary.
 select * from emp m where m.empno in(select mgr from emp) and m.sal>
 (select avg(e.sal) from emp e where e.mgr=m.empno)


96. List the name,salary,comm. For those employees whose net pay is greater than
or equal to any other employee salary of the company.
select ename,sal,comm from emp where (sal+isnull(comm,0)) >= any (select sal from emp)

97. Find out least 5 earners of the company.
select * from emp e where 5>(select count(*) from emp e where e.sal>sal)

98. Find out emps whose salaries greater than salaries of their managers.
select * from emp w,emp m where w.mgr=m.empno and w.sal>m.sal

99. List the managers who are not working under the president.
select * from emp where empno in(select mgr from emp) and mgr not in
 (select empno from emp where job = 'PRESIDENT')

100. List the records from emp whose deptno isnot in dept.
select * from dept where deptno not in(select deptno from emp)

101. List the Enames who are retiring after 31-Dec-89 the max Job period is 20Y.
select ename,hiredate from emp where hiredate <'12/31/89'

102. List those Emps whose Salary is odd value.

103. List the emp’s whose Salary contain 3 digits.
select * from emp where len(sal)=3

104. List the emps who joined in the month of DEC.
select * from emp where datepart(mm,hiredate)='12'

105. List the emps whose names contains ‘A’.
select * from emp where ename like'%a%'

106. List the emps Whose 10% of Salary is equal to year of joining.
select * from emp where datepart(yyyy,hiredate) in (select .1*sal from emp)

107. List first 50% of chars of Ename in Lower Case and remaining are upper Case.
select lower(substring(ename,1,round(len(ename)/2))) || substring(ename,round(len
(ename)/2)+1,len(ename))from emp

108. List the Dname whose No. of Emps is =to number of chars in the Dname.
select * from dept d where len(dname) in
(select count(*) from emp e where e.deptno = d.deptno )   --or
select d.dname,count(*) from emp e ,dept d where e.deptno = d.deptno
group by d.dname having count(*) = len (d.dname);

109. List the emps those who joined in company before 15th of the month.
select * from emp where datepart(dd,hiredate)<'15'

110. List the Dname, no of chars of which is = no. of emp’s in any other Dept.
select * from dept d where len(dname) in
(select count(*) from emp where d.deptno <> deptno group by deptno ) --or)
select * from dept d , (select count(*) s,e.deptno "M"from emp e group by e.deptno) d1
where len(dname)=d1.s and d1.M <>d.deptno;

111. List the emps who are working as Managers.
select * from emp where job = 'MANAGER'

112. List THE Name of dept where highest no.of emps are working.
select dname from dept where deptno in(select deptno from emp group by deptno
having count(*) in (select max(count(*)) from emp group by e.deptno)) --or

select dname from dept where deptno in
(select deptno from emp group by deptno
having count(*) in
(select max(count(*)) from emp group by deptno) );


113. List the emps who joined in the company on the same date.
select * from emp e where hiredate in
(select hiredate from emp where e.empno <> empno)

114. List the details of the emps whose Grade is equal to one tenth of Sales Dept.
select *  from emp e ,salgrade s where e.sal between s.losal and s.hisal and s.grade =0.1*
(select deptno from dept where dname='SALES')

115. List the name of the dept where more than average no. of emps are working.
select d.dname from dept d, emp e where e.deptno = d.deptno
group by d.dname
having count(*) > (select count(*),deptno from emp group by deptno); --wrong

116. List the Managers name who is having max no.of emps working under him.

select m.ename,count(*) from emp w,emp m
where w.mgr = m.empno
group by m.ename
having count(*) = (select max(count(*)) from emp group by mgr);
(OR)
B) select * from emp where empno = (select mgr from emp group by mgr having
 count(*) = (select max(count(*)) from emp group by mgr))


117) Print a list of emp’s Listing ‘just salary’ if Salary is more than 1500, on target if
Salary is 1500 and ‘Below 1500’ if Salary is less than 1500.
select empno,ename,sal,job,case
When sal =1500 then'ON TARGET'
WHEN SAL <1500 THEN 'BELOW 1500'
wHEN SAL>1500 THEN 'JUST SALARY'
ELSE 'NOTHING'
END "REVISED SALARY" FROM EMP

118) List those Managers who are getting less than his emps Salary.
SELECT * FROM EMP W WHERE EMPNO IN(SELECT MGR FROM EMP WHERE W.SAL<SAL)  --OR
select distinct m.ename,m.sal from emp w,emp m where w.mgr = m.empno and w.sal>m.sal

119) Print the details of all the emps who are sub-ordinates to Blake.
select * from emp where mgr in (select empno from emp where ename = 'BLAKE')

120) List the emps who are working as Managers using co-related sub-query.
select * from emp where empno in (select mgr from emp)

121) List the emps whose Mgr name is ‘Jones’ and also with his Manager name.
select w.ename,m.ename,(select ename from emp where m.mgr = empno) "his MANAGER"
from emp w,emp m where w.mgr = m.empno and m.ename = 'JONES' --or
 select e.ename,w.ename,m.ename from emp e,emp w,emp m where e.mgr = w.empno
 and w.ename = 'JONES' and w.mgr = m.empno

122) Define a variable representing the expression used to calculate on emps total annual
remuneration use the variable in a statement, which finds all emps who can earn 30000 a year or more.
A) Set define on
B) Define  annual = 12*nvl2(comm.,sal+comm.,sal)  (here define variable is a session variable)
C) Select * from emp where &annual > 30000;

123) Find out how may Managers are their in the company.
select count(*) from emp where job = 'MANAGER' --or
select count(*) from emp where empno in (select mgr from emp) --OR
select count(distinct m.empno) from emp w,emp m where w.mgr = m.empno

124) Find  Average  salary  and  Average  total  remuneration  for  each  Job  type.
Remember Salesman earn commission.secommm
 select avg(sal),avg(sal+ISNULL(comm,0)) from emp ;
select avg(sal),avg(sal+ISNULL(comm,0)) from emp where job='salesman'

125) Check whether all the emps numbers are indeed unique.
select empno,count(*) from emp group by empno

126) List the emps who are drawing less than 1000 Sort the output by Salary.
SELECT * FROM EMP WHERE SAL<1000 ORDER BY SAL

127) List the employee Name, Job, Annual Salary, deptno, Dept name and grade who
earn 36000 a year or who are not CLERKS.
SELECT E.ENAME,E.JOB,E.SAL*12 AS ANNUVALSAL,E.DEPTNO,D.DNAME,S.GRADE FROM EMP E,DEPT D,
SALGRADE S WHERE E.DEPTNO=D.DEPTNO
AND E.SAL BETWEEN S.LOSAL AND S.HISAL AND E.JOB <>'CLERK' AND E.SAL*12>=36000

128) Find out the Job that was filled in the first half of 1983 and same job
that was filled during the same period of 1984.
select * from emp where DATEPART(MM,HIREDATE) <= 06 and DATEPART(YY,HIREDATE) = 1984 and job in
 (select job from emp where DATEPART(MM,HIREDATE) <= 06 and DATEPART(YY,HIREDATE) <= 1983)

129) Find out the emps who joined in the company before their Managers.
select * from emp w,emp m where w.mgr = m.empno and
w.hiredate< m.hiredate   --OR
 select * from emp e where hiredate < (select hiredate from emp where empno = e.mgr)

130) Find all the emps who earn the minimum Salary for each job wise in ascending order.
select * from emp where sal in(select min(sal) from emp group by job) order by sal asc

131) Find  out  all  the  emps  who  earn  highest  salary  in  each  job  type.
Sort  in descending salary order.
select * from emp where sal in(select max(sal) from emp group by job) order by sal desc

132) Find out the most recently hired emps in each Dept order by Hiredate.
select * from emp e where hiredate in(select max(hiredate) from emp where e.deptno=deptno )
order by hiredate asc

133) List the employee name,Salary and Deptno for each employee who earns a salary
greater than the average for their department order by Deptno.           
select * from emp e where sal>(select avg(sal) from emp e where e.deptno=deptno)order by deptno asc

134) List the Deptno where there are no emps.
select deptno,count(*) from emp group by deptno having count(*)=0
           
135) List the No.of emp’s and Avg salary within each department for each job.
select count(ename),avg(sal),deptno,job from emp group by deptno,job

136) Find the maximum average salary drawn for each job except for ‘President’.
select max(avg(sal)) from emp where job not in('president') group by job    --wrong
select max(sal),avg(sal),job from emp where job not in('president') group by job

137) Find the name and Job of the emps who earn Max salary and Commission.
select * from emp where sal = (select max(sal) from emp) and comm is not null
           
138) List  the  Name,  Job  and  Salary  of the  emps  who  are  not  belonging  to  the department 10
but who have the same job and Salary as the emps of dept 10.
select ename,job,sal from emp where deptno <>'10' and job in(select job from emp where deptno=10)
and sal in(select sal from emp where deptno=10)

139) List the Deptno, Name, Job, Salary and Sal+Comm of the SALESMAN who are earning
maximum salary and commission in descending order.
select deptno,sal,(sal+isnull(comm,0)) as tsal,job,comm from emp where job='salesman'
and sal in(select max(sal+isnull(comm,0)) from emp where comm is not null)order by (sal+isnull(comm,0)) desc

140) List the Deptno, Name, Job, Salary and Sal+Comm of the emps who earn the
second highest earnings (sal + comm.).
select top(2) sal ,deptno,ename,job,(sal+isnull(comm,0))as tsal from emp

select deptno,ename,sal,job,(sal+isnull(comm,0))as tsal from emp e where 2 =
(select count(distinct (sal+isnull(comm,0)) )
from emp where (e.sal+isnull(comm,0))<(e.sal+isnull(comm,0)))         

141) List the Deptno and their average salaries for dept with the average salary less than
the averages for all department
select deptno,avg(sal) from emp group by deptno
having avg(sal) <(select avg(Sal) from emp)

142)list the Names and Salaries of the emps along with their manager names
and salaries for those emps who earn more salary than their Manager.
select w.ename,w.sal,m.ename,m.sal from emp w,emp m
where w.mgr = m.empno and w.sal > m.sal

143) list t the Name, Job, Salary of the emps in the department with the highest average salary.
select * from emp where deptno in (select deptno from emp e having avg(sal)=
(select max(avg(sal)) from emp group by deptno ))group by deptno   --wrong

144List the empno,sal,comm. Of emps.
select empno,sal,comm from emp

145List the details of the emps in the ascending order of the sal.
select * from emp order by sal asc

146List the dept in the ascending order of the job and the desc order of the emps print empno, ename.
select * from emp order by job asc,empno desc

147Display the unique dept of the emps.
select * from dept where deptno in (select distinct(deptno) from emp)

148Display the unique dept with jobs.
select distinct(job),deptno from emp

149Display the details of the blake.
select * from emp where ename='blake'


150) List all the clerks.
select * from emp where job='clerk'

151) list all the employees joined on 1st may 81.
select * from emp where hiredate ='05/01/1981'

152) List the empno,ename,sal,deptno of the dept 10 emps in the ascending order of salary.
select * from emp where deptno=10 order by sal asc

153) List the emps whose salaries are less than 3500.
select * from emp where sal<3500 order by sal desc

154) List the empno,ename,sal of all the emp joined before 1 apr 81.
select * from emp where hiredate <'04/01/1981'

155) List the emp whose annual sal is <25000 in the asc order of the salaries.
select *,sal*12 as tsal from emp where sal*12 <25000 order by sal asc

156) List the empno,ename,annsal,dailysal  of all the salesmen in the asc ann sal
select *,sal/30 as dailysal,sal*12 as annuvalsal from emp where job='salesman' order by sal*12 asc
           
157) List the empno,ename,hiredate,current date & exp in the ascending order of the exp.
select hiredate,empno,ename,getdate() as todaydate,datediff(yy,hiredate,getdate()) as years from emp order by
datediff(yy,hiredate,getdate()) asc

158) List the emps whose exp is more than 10 years.
select * from emp where datediff(yy,hiredate,getdate())>10

159) List the emps who are working as managers.
select * from emp where job='manager'

160) List the emps who are either clerks or managers.
select * from emp where job in('clerk','manager')

161) List the emps who have joined on the following dates 1 may 81,17 nov 81,30 dec 81
select * from emp where hiredate in ('05/01/1981','11/17/81','12/30/81')

162) List the emps who have joined in the year 1981.
select * from emp where datepart(yyyy,hiredate)='1981'

163) List the emps whose annual sal ranging from 23000 to 40000.
select sal*12,* from emp where sal*12 between 23000 and 40000

164) List the emps working under the mgrs 7369,7890,7654,7900.
select * from emp where mgr in('7369','7890','7654','7900')

165)  List all the 4char emps.
select * from emp where len(ename)=4

166) List the emp names starting with ‘M’ with 5 chars.
select * from emp where ename like'm%' and len(ename)=5

167) List the emps end with ‘H’ all together 5 chars.
select * from emp where ename like'%h' and len(ename)=5

168) List names start with ‘M’.
select * from emp where ename like'm%'

169) List the emps who joined in the year 81.
select * from emp where datepart(yyyy,hiredate)='1981'

170) List the emps whose sal is ending with 00.
select * from emp where sal like''

171) List the emp who joined in the month of JAN.
select * from emp where datepart(mm,hiredate)='01'

172) Who joined in the month having char ‘a’.
select * from emp where hiredate like'%a%'
           
173) Who joined in the month having second char ‘a’
select * from emp where hiredate like'_a%'

174)  List the emps whose salary is 4 digit number.
select * from emp where len (sal)=4

175) List the emp who joined in 80’s.
select * from emp where datepart(yyyy,hiredate)='1980'

176) List the emp who are clerks who have exp more than 8ys.
select * from emp where job='clerk' and datediff(yyyy,hiredate,getdate())>8

178) List the mgrs of dept 10 or 20.
select * from emp where deptno in(10,20) and job='manager'

179) List the emps joined in jan with salary ranging from 1500 to 4000.
select * from emp where sal between 1500 and 4000 and datepart(mm,hiredate)='01'

180) List the unique jobs of dept 20 and 30 in desc order.
select distinct(job) from emp where deptno in(20,30) order by job desc

181) List the emps along with exp of those working under the mgr whose number is starting with
7 but should not have a 9 joined before 1983.
select * from emp where (mgr like '7%' and mgr not like '%9%')  and datediff(yyyy,hiredate,getdate())<1983

182) List the emps who are working as either mgr or analyst with the salary ranging from 2000 to 5000
and with out comm.
select * from emp where job in('manager','analyst') and sal between 2000 and 5000 and comm is null

183) List the empno,ename,sal,job of the emps with /ann sal <34000 but receiving some comm.
Which should not be>sal and desg should be sales man working for dept 30.
select empno,ename,sal,job,deptno,sal*12 as annauvalsal from emp where sal*12<34000
and comm is not null and comm>sal  and job='salesman'and deptno=30
207) List the emps who are working for dept 10 or 20 with desgs as clerk or analyst with a sal is either
3 or 4 digits with an exp>8ys but does not belong to mons of mar,apr,sep and working for mgrs &no  is not ending with 88 and 56.
select * from emp where deptno in(10,20) and job in('clerk','analyst') and len (sal)in(3,4)
and datediff(yyyy,hiredate,getdate())>8 and datepart(mm,hiredate)in('03','04','09') and mgr not like
('%88')and mgr not like ('%56')

184) List the empno,ename,sal,job,deptno&exp of all the emps belongs to dept 10 or20 with an exp 6 to 10 y working under the same mgr with out comm. With a job not ending irrespective of the position with comm.>200 with exp>=7y and sal<2500 but not belongs to the month sep or nov working under the mgr whose no is not having digits either 9 or 0 in the asc dept& desc dept
select empno,ename,sal,job,deptno,datediff(yyyy,hiredate,getdate()) as exp from emp where deptno in(10,20)
and datediff(yyyy,hiredate,getdate()) between 6 and 10                      --wrong

185) List the details of the emps working at Chicago.
select * from emp where deptno in(select deptno from dept where dept.loc='chicago')

186) List the empno,ename,deptno,loc of all the emps.
select e.empno,e.ename,e.deptno,d.loc from emp e ,dept d where e.deptno = d.deptno

187) List the empno,ename,loc,dname of all the depts 10 and 20.
select * from emp e,dept d where e.deptno=d.deptno and e.deptno in(10,20)

188) List the empno, ename, sal, loc of the emps working at Chicago dallas with an exp>6ys.
select * from emp e,dept d where e.deptno=d.deptno and d.loc in('chicago','dallas') and
datediff(yyyy,hiredate,getdate()) >6

189) List the emps along with loc of those who belongs to dallas ,
newyork with sal ranging from 2000 to 5000 joined in 81.
select * from emp e,dept d where e.deptno=d.deptno and d.loc in('dallas','newyork') and
e.sal between 2000 and 5000 and datepart(yyyy,hiredate)='1981'

190) List the empno,ename,sal,grade of all emps.
select * from emp e,salgrade s where e.sal between s.losal and s.hisal

191) List the grade 2 and 3 emp of Chicago.
select * from emp e,dept d,salgrade s where e.deptno=d.deptno and
e.sal between s.losal and s.hisal and d.loc='chicago' and s.GRADE IN(2,3)

192) List the emps with loc and grade of  accounting dept or the locs dallas or
Chicago with the grades 3 to 5 &exp >6y
select e.deptno,e.empno,e.ename,e.sal,d.dname,d.loc,s.grade from emp e,salgrade s,dept d
where e.deptno = d.deptno and e.sal between s.losal and s.hisal
and s.grade in (3,5)
and datediff(yyyy,hiredate,getdate()) >6
and ( d.dname = 'ACCOUNTING' or D.loc in ('DALLAS','CHICAGO'))

193) List the grades 3 emps of research and operations depts joined after 1987 and
whose names should not be either miller or allen.
select e.ename from emp e ,dept d,salgrade s where e.deptno = d.deptno and
d.dname in ('OPERATIONS','RESEARCH') and e.sal between s.losal and s.hisal
and e.ename not in ('MILLER','ALLEN')
and datediff(yyyy,hiredate,getdate()) >'1987'

194) List the emps whose job is same as smith.
select * from EMP where JOB=(select JOB from EMP where ENAME='smith')

195)  List the emps who are senior to miller
select * from emp where HIREDATE<(select HIREDATE from EMP where ENAME='miller')

196) List the emps whose job is same as either allen or sal>allen.
select * from EMP where JOB=(select JOB from EMP where ENAME='allen') OR
SAL>(select sal from emp where ename='allen')

197) List the emps who are senior to their own manager.
 select * from emp w,emp m where w.mgr = m.empno and
w.hiredate < m.hiredate

198) List the emps whose sal greater than blakes sal.
select * from emp where sal<(select sal from EMP where ENAME='blake')

199) List the dept 10 emps whose sal>allen sal.
select * from emp where deptno=10 and sal>(select sal from EMP where ENAME='allen')

200) List the mgrs who are senior to king and who are junior to smith.
SELECT * FROM EMP WHERE EMPNO IN(select mgr from emp  where hiredate<(select hiredate from
emp where ename = 'KING' )  and hiredate > (select hiredate from emp
where ename = 'SMITH')) and mgr is not null

201) List the empno,ename,loc,sal,dname,loc of the all the emps belonging to king dept.
 select e.*,d.* from emp e,dept d  where e.deptno=d.deptno
and e.deptno in  (select deptno from emp where ename = 'KING'and emp.empno <> e.empno)

202) List the emps whose salgrade are greater than the grade of miller.
select * from emp e,salgrade s
where e.sal between s.losal and s.hisal and s.grade >
(select s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal and e.ename = 'MILLER')

203) List the emps whose sal is same as ford or blake.
select * from emp where sal in (select sal from emp e where e.ename in
('FORD','BLAKE')and emp.empno <> e.empno)

204) List the emps whose sal is same as any one of the following.
select * from emp where sal in
(select sal from emp e where emp.empno <> e.empno)

205) Sal of any clerk of emp1 table.
select * from EMP where JOB='clerk'

206) Any emp of emp2 joined before 82.
select * from EMP where DATEpart(yyyy,hiredate)='1982'

207) The total remuneration (sal+comm.) of all sales person of Sales dept belonging to emp3 table.
select * from emp e
where (sal+isnull(comm,0)) in
(select sal+isnull(comm,0) from emp e,dept d where e.deptno=d.deptno
and d.dname = 'SALES'and e.job = 'SALESMAN')

208) ggGrade 4 emps Sal of emp 4 table.
select * from EMP e,SALGRADE s where e.SAL between s.LOSAL and s.HISAL and s.GRADE=4

209) Any emp Sal of emp5 table.
select * from emp

210) List the highest paid emp.
select * from emp where sal in (select max(sal) from emp);

211) List the details of most recently hired emp of dept 30.
 select * from emp where hiredate in  (select max(hiredate) from emp where deptno = 30)
           
212) List the highest paid emp of Chicago joined before the most  recently hired emp of grade 2.
select * from emp where sal = ( select max(sal) from emp e,dept d where e.deptno = d.deptno
and d.loc = 'CHICAGO' and hiredate <(select max(hiredate) from emp e ,salgrade s where
e.sal between s.losal and s.hisal and s.grade = 2))

213) List the highest paid emp working under king.
select * from emp where sal in  (select max(sal) from emp where mgr in

 (select empno from emp where ename = 'KING'))