0% found this document useful (0 votes)
9 views4 pages

Queries Examples

The document provides various SQL query examples for retrieving employee data based on different conditions, such as location, hire dates, and salary comparisons. It explains the use of operators like NOT, BETWEEN, IN, and LIKE for filtering results, as well as sorting results using the ORDER BY clause. Additionally, it includes examples of subqueries for more complex data retrieval scenarios.

Uploaded by

vreena3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Queries Examples

The document provides various SQL query examples for retrieving employee data based on different conditions, such as location, hire dates, and salary comparisons. It explains the use of operators like NOT, BETWEEN, IN, and LIKE for filtering results, as well as sorting results using the ORDER BY clause. Additionally, it includes examples of subqueries for more complex data retrieval scenarios.

Uploaded by

vreena3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Queries Examples:

a) If you wanted to get the opposite, the employees who do not live in London, you would
write:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


WHERE City <> 'London'

b) It is not necessary to test for equality; you can also use the standard equality/inequality
operators that you would expect. For example, to get a list of employees who where hired
on or after a given date, you would write:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


WHERE HireDate >= '1-july-1993'

c) If we want to know which employees were hired between two given dates, we could write:

SELECT EmployeeID, FirstName, LastName, HireDate, City


FROM Employees
WHERE (HireDate >= '1-june-1992') AND (HireDate <= '15-december-1993')

d) Note that SQL also has a special BETWEEN operator that checks to see if a value is between
two values (including equality on both ends). This allows us to rewrite the previous query
as:

SELECT EmployeeID, FirstName, LastName, HireDate, City


FROM Employees
WHERE HireDate BETWEEN '1-june-1992' AND '15-december-1993'

e) We could also use the NOT operator, to fetch those rows that are not between the specified
dates:

SELECT EmployeeID, FirstName, LastName, HireDate, City


FROM Employees
WHERE HireDate NOT BETWEEN '1-june-1992' AND '15-december-1993'

f) What if we want to check if a column value is equal to more than one value? If it is only 2
values, then it is easy enough to test for each of those values, combining them with the OR
operator and writing something like:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


WHERE City = 'London' OR City = 'Seattle'
g) However, if there are three, four, or more values that we want to compare against, the
above approach quickly becomes messy. In such cases, we can use the IN operator to test
against a set of values. If we wanted to see if the City was either Seattle, Tacoma, or
Redmond, we would write:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


WHERE City IN ('Seattle', 'Tacoma', 'Redmond')

h) As with the BETWEEN operator, here too we can reverse the results obtained and query for
those rows where City is not in the specified list:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees

WHERE City NOT IN ('Seattle', 'Tacoma', 'Redmond')

i) Finally, the LIKE operator allows us to perform basic pattern-matching using wildcard
characters. For Microsoft SQL Server, the wildcard characters are defined as follows:

Wildcard Description
_ (underscore) matches any single character
% matches a string of one or more characters
matches any single character within the specified range (e.g. [a-f]) or set (e.g.
[]
[abcdef]).
matches any single character not within the specified range (e.g. [^a-f]) or set
[^]
(e.g. [^abcdef]).

A few examples should help clarify these rules.

 WHERE FirstName LIKE '_im' finds all three-letter first names that end with 'im' (e.g. Jim,
Tim).
 WHERE LastName LIKE '%stein' finds all employees whose last name ends with 'stein'
 WHERE LastName LIKE '%stein%' finds all employees whose last name includes 'stein'
anywhere in the name.
 WHERE FirstName LIKE '[JT]im' finds three-letter first names that end with 'im' and begin
with either 'J' or 'T' (that is, only Jim and Tim)
 WHERE LastName LIKE 'm[^c]%' finds all last names beginning with 'm' where the following
(second) letter is not 'c'.
j) Here too, we can opt to use the NOT operator: to find all of the employees whose first
name does not start with 'M' or 'A', we would write:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


WHERE (FirstName NOT LIKE 'M%') AND (FirstName NOT LIKE 'A%')
k) To sort the data rows, we include the ORDER BY clause. The ORDER BY clause includes one
or more column names that specify the sort order. If we return to one of our first SELECT
statements, we can sort its results by City with the following statement:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


ORDER BY City

l) If we want the sort order for a column to be descending, we can include the DESC keyword
after the column name.The ORDER BY clause is not limited to a single column. You can
include a comma-delimited list of columns to sort by—the rows will all be sorted by the first
column specified and then by the next column specified. If we add the Country field to the
SELECT clause and want to sort by Country and City, we would write:

SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM Employees ORDER BY
Country, City DESC
Or,

SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM Employees


ORDER BY Country ASC, City DESC

m) It is important to note that a column does not need to be included in the list of selected
(returned) columns in order to be used in the ORDER BY clause. If we don't need to see/use
the Country values, but are only interested in them as the primary sorting field we could
write the query as:

SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees


ORDER BY Country ASC, City DESC

n) You need to find the salaries for all the employees who have a higher salary than the Vice
President of a company 'ABC'.Which of the following queries will give you the required
result? (Consider the table structure as given):
SQL> DESC employees
Name Null? Type
----------------------- -------- ----------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)

Ans- Select First_Name, Last_Name, salary from Employee

Where salary > (Select salary from employees where job_id = 'VICE-PRESIDENT');

o) SELECT first_name, last_name, min(salary)


FROM employees
GROUP BY department_id
HAVING MIN(salary) >
(SELECT min(salary)
FROM employees
Where department_Id-100);

Ans: It executes successfully and gives the names and minimum salary greater than department 100 of all
employees

p) SELECT last_name, job_id, salary


FROM employees
WHERE salary = (SELECT max(salary) from employees);

Ans: It executes successfully and gives the employees who have salaries equal to the max salary.

You might also like