Sie sind auf Seite 1von 57

Exercitii Oracle SQL

338 exerciti

1
Contents
Installation Process ...................................................................................................................................... 3
Install Oracle Express edition 11g ............................................................................................................ 3
Oracle 12c ................................................................................................................................................ 5
Install SQL Developer and JDK ................................................................................................................. 5
Working with Oracle without installing anything ......................................................................................... 6
Login as SYSDBA ................................................................................................................................... 14
Create user learn .................................................................................................................................... 15
Create Learn connection and Schema. ................................................................................................... 16
Tabelele pt exercitii..................................................................................................................................... 17
Basic retrieving data from tables [33 Exercises] ......................................................................................... 17
Boolean and Relational operators [12 Exercises]........................................................................................ 20
Wildcard and Special operators [22 Exercises] ........................................................................................... 22
Aggregate Functions [25 Exercises] ........................................................................................................... 24
SQL Formatting query output [10 Exercises] .............................................................................................. 27
SQL Quering on Multiple Tables [7 Exercises] ............................................................................................ 28
FILTERING and SORTING on HR Database [38 Exercises] ...................................................................... 29
SQL JOINS [29 Exercises] ......................................................................................................................... 33
SQL JOINS on HR Database [27 Exercises] .............................................................................................. 36
SQL SUBQUERIES [39 Exercises] ............................................................................................................. 39
SQL SUBQUERIES on HR Database [55 Exercises] .................................................................................. 43
SQL Union[9 Exercises].............................................................................................................................. 50
SQL View [16 Exercises] ............................................................................................................................ 51
SQL User Account Management [16 Exercise] ........................................................................................... 52

2
Installation Process

Install Oracle Express edition 11g


https://login.oracle.com
Create account and login.

3
4
Oracle 12c

It is too large 2.8 Gb and for what we need the Express Edition is ok

Install SQL Developer and JDK

http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html

5
Working with Oracle without installing anything

There is another Oracle site you can use to run SQL commands without installing a database and a client
tool like SQL Developer.

The process of setting it up is just a little more complex than using Live SQL, but there are some differences
that you might like, so if you want to give it a go, here is a detailed guide on how to create your account
there.

 First, go to apex.oracle.com

 Once there, click on the “Get Started” button:

 In the “Get started” page select the “Free Workspace” option:

6
 In the first step of the “Request Workspace” wizard select the “Application Development” option
and click “Next":

7
 In the “Identification” step enter your details. In the workspace field, write the name you want to use
for your workspace. It must be unique, so it is usually a good idea to use a name that include your
initials or part of your name:

8
 In the next step write the name you would like for your schema. The schema is just the name of the
database user that will own the objects you create in your workspace. You can click on the question
mark in case of doubts about how to choose the name. For the space allocation, 10MB should be
enough.

 In the next step just answer the 3 questions as you wish.

9
 Read and accept the terms of the service.

 In the last step just review your details and click on “Submit Request”.

10
 You will get a confirmation screen, telling you that an email will be sent to you once the workspace
is created.

 The e-mail you will receive will be similar to this one. To complete the process click on the link at
the end of the message:

11
 Once the workspace is created you will be asked to change your password, so set a new password
that you won’t forget.

 After changing the password you will be logged in to your workspace. Select the "SQL Workshop"
option.

12
 In the SQL Workshop you can select “SQL Commands” to run SQL code, or “SQL Scripts” to
upload the scripts needed for the course when some lecture mentions that a script needs to be run:

And that's it!

The main benefit of using apex instead of Live SQL, is that the tables or objects you create in apex, do
persist from one session to another, whereas on Live SQL you need to create the tables again every
time you start a new session.

This means that if you use apex, you don't need to run the scripts that were designed for Live SQL, but the
ones that would be used if you were running a local database. Also, you won't need to run a script every
time you start a new session.

On the other hand, you cannot run more than one SQL command in one go in apex. If you have more
than one SQL statement in the SQL Workshop and you press CTRL+ENTER or click on the “Run”
button, you will get an error. You can have more than one statement there but you have to run only
one at a time, and you do that by selecting or marking the command you want to run (as in the image
below) and then pressing CTRL+ENTER or clicking on the “Run” button.

13
Login as SYSDBA
Create a connection.

14
Connection Name: Is up to you but will be good SYS or SYSDBA
User name and password: the one you set up at the installation of
Oracle Express
Hostname: is localhost because you are using you station
Port:1521
Sid: is ex from express editions if you install other package you might
have to change the SID
IMPORTANT for you SYS you need to select Role SYSDBA.

Create user learn

After you logged in as a SYS administrator you have to create a new user. From that user you will work not
from sys account.

CREATE USER learn IDENTIFIED BY learn; -- User creation


GRANT CREATE SESSION to learn; -- Necessary to connect to the database
GRANT CREATE ANY TABLE to learn; -- Necessary to be able to create tables
GRANT RESOURCE to learn; -- Necessary to extend the system tablespace
GRANT SELECT_CATALOG_ROLE to learn; -- Necessary to be able to AutoTrace commands
GRANT CREATE ANY VIEW TO learn; -- Necessary to be able to create views
GRANT UNLIMITED TABLESPACE TO learn; -- Necessary to extend a tablespace when needed
GRANT CONNECT TO username;
You find this on the file UserCreation-script.txt and copy and paste in Oracle developer.
15
Disconnect from SYS.
Right click on SYS and Disconnect.

Create Learn connection and Schema.

16
Create connection as you see on the image.

And Save

Tabelele pt exercitii
Create Shema
Create tables that we need for exercises. From file TablesCreation-script.txt copy and paste all text and run
it. Now you have all the table for the exercises.

Basic retrieving data from tables [33 Exercises]

1. Write a SQL statement to display all the information of all salesmen

Table: salesman

2. Write a SQL statement to display a string "This is SQL Exercise, Practice and
Solution".

3. Write a query to display three numbers in three columns.

4. Write a query to display the sum of two numbers 10 and 15 from RDMS sever.

5. Write a query to display the result of an arithmetic expression.

17
6. Write a SQL statement to display specific columns like name and commission for all the
salesmen.
Table: salesman

7. Write a query to display the columns in a specific order like order date, salesman id,
order number and purchase amount from for all the orders

Table : orders

8. Write a query which will retrieve the value of salesman id of all salesmen, getting orders
from the customers in orders table without any repeats

Table: orders

9. Write a SQL statement to display names and city of salesman, who belongs to the city of
Paris

Table: salesman

10. Write a SQL statement to display all the information for those customers with a grade
of 200

Table: customer

11. Write a SQL query to display the order number followed by order date and the
purchase amount for each order which will be delivered by the salesman who is holding
the ID 5001.

Table: orders

12. Write a SQL query to display the Nobel prizes for 1970.

Table: nobel_win

13. Write a SQL query to know the winner of the 1971 prize for Literature.

Table: nobel_win

14. Write a SQL query to display the year and subject that won 'Dennis Gabor' his prize.

Table: nobel_win

15. Write a SQL query to give the name of the 'Physics' winners since the year 1950.

Table: nobel_win

18
16. Write a SQL query to Show all the details (year, subject, winner, country ) of the
Chemistry prize winners between the year 1965 to 1975 inclusive.

Table: nobel_win

17. Write a SQL query to show all details of the Prime Ministerial winners after 1972 of
Menachem Begin and Yitzhak Rabin.

Table: nobel_win

18. Write a SQL query to show all the details of the winners with first name Louis.

Table: nobel_win

19. Write a SQL query to show all the winners in Physics for 1970 together with the winner
of Economics for 1971.

Table: nobel_win

20. Write a SQL query to show all the winners of nobel prize in the year 1970 except the
subject Physiology and Economics.

Table: nobel_win

21. Write a SQL query to show the winners of a 'Physiology' prize in an early year before
1971 together with winners of a 'Peace' prize in a later year on and after the 1974.

Table: nobel_win

22. Write a SQL query to find all details of the prize won by Johannes Georg Bednorz.

Table: nobel_win

23. Write a SQL query to find all the details of the nobel winners for the subject not started
with the letter 'P' and arranged the list as the most recent comes first, then by name in
order.

Table: nobel_win

24. Write a SQL query to find all the details of 1970 winners by the ordered to subject and
winner name; but the list contain the subject Economics and Chemistry at last.

Table: nobel_win

25. Write a SQL query to find all the products with a price between Rs.200 and Rs.600.

Table: item_mast
19
26. Write a SQL query to calculate the average price of all products of the manufacturer
which code is l6.

Table: item_mast

27. Write a SQL query to find the item name and price in Rs.

Table: item_mast

28. Write a SQL query to display the name and price of all the items with a price is equal or
more than Rs.250, and the list contain the larger price first and then by name in ascending
order.

Table: item_mast

29. Write a SQL query to display the average price of the items for each company,
showing only the company code.

Table: item_mast

30. Write a SQL query to find the name and price of the cheapest item.

Table: item_mast

31. Write a query in SQL to find the last name of all employees, without duplicates.

Table: emp_details

32. Write a query in SQL to find the data of employees whose last name is 'Snares'.

Table: emp_details

33. Write a query in SQL to display all the data of employees that work in the department
57.

Table: emp_details

Boolean and Relational operators [12 Exercises]

1. Write a query to display all customers with a grade above 100.

Table: customer

2. Write a query statement to display all customers in New York who have a grade value
above 100.

Table: customer
20
3. Write a SQL statement to display all customers, who are either belongs to the city New
York or had a grade above 100

Table: customer

4. Write a SQL statement to display all the customers, who are either belongs to the city
New York or not had a grade above 100.

Table: customer

5.Write a SQL query to display those customers who are neither belongs to the city New
York nor grade value is more than 100.

Table: customer

6. Write a SQL statement to display either those orders which are not issued on date 2012-
09-10 and issued by the salesman whose ID is 505 and below or those orders which
purchase amount is 1000.00 and below.

Table : orders

7.Write a SQL statement to display salesman_id, name, city and commission who gets the
commission within the range more than 0.10% and less than 0.12%.

Table : salesman

8. Write a SQL query to display all orders where purchase amount less than a specified
amount or order date and customer_id must not be greater than a specified data and less
than a specified ID respectively.

Table : orders

9. Display all in reverse, where order dates equal to a specified date or customer id greater
than a specified number and purchase amount less than a specified amount.

Table : orders

10. Write a SQL query to display order number, purchase amount, archived, the
unachieved percentage for those orders which exceeds the 50% of the target value of
6000.

Table: orders

11. Write a query in SQL to find the data of employees whose last name is Dosni or
Mardy.

Table : emp_details
21
12. Write a query in SQL to display all the data of employees that work in department 47 or
department 63.

Table : emp_details

Wildcard and Special operators [22 Exercises]


1. Write a SQL statement to find those salesmen with all information who come from the
city either Paris or Rome.

Table: salesman

2. Write a query to filter those salesmen with all information who comes from any of the
cities Paris and Rome.

Table: salesman

3. Write a query to filter those salesmen with all information who likes to leave other cities
than Paris and Rome.

Table: salesman

4. Write a query to sort out those customers with all information whose ID value is within
any of 3007, 3008 and 3009.

Table: customer

5. Write a SQL statement to find those salesmen with all information who gets the
commission within a range of 0.12 and 0.14.

Table: salesman

6. Write a query to filter all those orders with all information which purchase amount value
is within the range 500 and 4000 except those orders of purchase amount value 948.50
and 1983.43.

Table: orders

7. Write a SQL statement to find those salesmen with all other information and name
started with any latter 'A' and 'L'.

Table: salesman

8. Write a SQL statement to find those salesmen with all other information and name
started with other than any latter within 'A' and 'L'.

Table: salesman

22
9. Write a SQL statement to find that customer with all information whose name begin with
the letter 'P'.

Table: customer

10. Write a SQL statement to find all those customers with all information whose names
are ending with the letter 'n'.

Table: customer

11. Write a SQL statement to find those salesmen with all information whose name
containing the 1st character is 'N' and the 4th character is 'l' and rests may be any
character.

Table : salesman

12. Write a SQL statement to find those rows from the table testtable which contain the
escape character underscore ( _ ) in its column 'col1'.

Table: testtable

13. Write a SQL statement to find those rows from the table testtable which does not
contain the character underscore ( _ ) in its column 'col1'.

Table: testtable

14. Write a SQL statement to find those rows from the table testtable which contain the
escape character ( / ) in its column 'col1'.

Table: testtable

15. Write a SQL statement to find those rows from the table testtable which does not
contain the escape character ( / ) in its column 'col1'.

Table: testtable

16. Write a SQL statement to find those rows from the table testtable which contain the
string ( _/ ) in its column 'col1'.

Table: testtable

17. Write a SQL statement to find those rows from the table testtable which does not
contain the string ( _/ ) in its column 'col1'.

Table: testtable

23
18. Write a SQL statement to find those rows from the table testtable which contain the
character ( % ) in its column 'col1'.

Table: testtable

19. Write a SQL statement to find those rows from the table testtable which does not
contain the character ( % ) in its column 'col1'.

Table: testtable

20. Write a SQL statement to find that customer with all information who does not get any
grade except NULL.

Table: customer

21. Write a SQL statement to find that customer with all information who gets a grade
except NULL value.

Table: customer

22. Write a query in SQL to display all the data of employees whose last name begins with
an 'D'.

Table: emp_details

Aggregate Functions [25 Exercises]


1. Write a SQL statement to find the total purchase amount of all orders.

Table: orders

2. Write a SQL statement to find the average purchase amount of all orders.

Table: orders

3. Write a SQL statement to find the number of salesmen currently listing for all of their
customers.

Table: orders

4. Write a SQL statement know how many customer have listed their names.

Table: customer

5. Write a SQL statement find the number of customers who gets at least a gradation for
his/her performance.
24
Table: customer

6. Write a SQL statement to get the maximum purchase amount of all the orders.

Table: orders

7. Write a SQL statement to get the minimum purchase amount of all the orders.

Table: orders

8. Write a SQL statement which selects the highest grade for each of the cities of the
customers.

Table: customer

9. Write a SQL statement to find the highest purchase amount ordered by the each
customer with their ID and highest purchase amount.

Table: orders

10. Write a SQL statement to find the highest purchase amount ordered by the each
customer on a particular date with their ID, order date and highest purchase amount.

Table: orders

11. Write a SQL statement to find the highest purchase amount on a date '2012-08-17' for
each salesman with their ID.

Table: orders

12. Write a SQL statement to find the highest purchase amount with their ID and order
date, for only those customers who have highest purchase amount in a day is more than
2000.

Table: orders

13. Write a SQL statement to find the highest purchase amount with their ID and order
date, for those customers who have a higher purchase amount in a day is within the range
2000 and 6000.

Table: orders

14. Write a SQL statement to find the highest purchase amount with their ID and order
date, for only those customers who have a higher purchase amount in a day is within the
list 2000, 3000, 5760 and 6000.

Table: orders
25
15. Write a SQL statement to find the highest purchase amount with their ID, for only those
customers whose ID is within the range 3002 and 3007.

Table: orders

16. Write a SQL statement to display customer details (ID and purchase amount) whose
IDs are within the range 3002 and 3007 and highest purchase amount is more than 1000.

Table: orders

17. Write a SQL statement to find the highest purchase amount with their ID, for only those
salesmen whose ID is within the range 5003 and 5008.

Table: orders

18. Write a SQL statement that counts all orders for a date August 17th, 2012.

Table: orders

19. Write a SQL statement that counts the number of different non NULL city values for
salesmen.

Table: salesman

20. Write a query that counts the number of salesmen with their order date and ID
registering orders for each day.

Table: orders

21. Write a SQL query to calculate the average price of all the products.

Table: item_mast

22. Write a SQL query to find the number of products with a price more than or equal to
Rs.350.

Table: item_mast

23. Write a SQL query to display the average price of each company's products, along with
their code.

Table: item_mast

24. Write a query in SQL to find the sum of the allotment amount of all departments.

Table: emp_department

26
25. Write a query in SQL to find the number of employees in each department along with
the department code.

Table: emp_details

SQL Formatting query output [10 Exercises]

1. Write a SQL statement to display the commission with the percent sign ( % ) with
salesman ID, name and city columns for all the salesmen.

Table: salesman

2. Write a SQL statement to find out the number of orders booked for each day and display
it in such a format like "For 2001-10-10 there are 15 orders".

Table: orders

3. Write a query to display the orders according to the order number arranged by
ascending order.

Table: orders

4. Write a SQL statement to arrange the orders according to the order date in such a
manner that the latest date will come first then previous dates.

Table: orders

5. Write a SQL statement to display the orders with all information in such a manner that,
the older order date will come first and the highest purchase amount of same day will come
first.

Table: orders

6. Write a SQL statement to display the customer name, city, and grade, etc. and the
display will be arranged according to the smallest customer ID.

Table: customer

7. Write a SQL statement to make a report with salesman ID, order date and highest
purchase amount in such an arrangement that, the smallest salesman ID will come first
along with their smallest order date.

Table: orders

27
8. Write a SQL statement to display customer name, city and grade in such a manner that,
the customer holding highest grade will come first.

Table: customer

9. Write a SQL statement to make a report with customer ID in such a manner that, the
largest number of orders booked by the customer will come first along with their highest
purchase amount.

Table: orders

10. Write a SQL statement to make a report with order date in such a manner that, the
latest order date will come first along with the total purchase amount and total commission
(15% for all salesmen) for that date.

Table : orders

SQL Quering on Multiple Tables [7 Exercises]


1. Write a query to find those customers with their name and those salesmen with their
name and city who lives in the same city.

Table: salesman

Table: customer

2. Write a SQL statement to find the names of all customers along with the salesmen who
works for them.

Table: customer, salesman

3. Write a SQL statement to display all those orders by the customers not located in the
same cities where their salesmen live.

Table: salesman, customer, orders

4. Write a SQL statement that finds out each order number followed by the name of the
customers who made the order.

Table: orders, customer

5. Write a SQL statement that sorts out the customer and their grade who made an order.
Each of the customers must have a grade and served by at least a salesman, who belongs
to a city.

Table: salesman, customer, orders

28
6. Write a query that produces all customers with their name, city, salesman and
commission, who served by a salesman and the salesman works at a rate of the
commission within 12% to 14%.

Table: salesman, customer

7. Write a SQL statement that produces all orders with the order number, customer name,
commission rate and earned commission amount for those customers who carry their
grade more than 200 and served by an existing salesman.

Table: salesman, customer, orders

FILTERING and SORTING on HR Database [38 Exercises]


1. Write a query in SQL to display the full name (first and last name), and salary for those
employees who earn below 6000.

Table: employees

2. Write a query in SQL to display the first and last_name, department number and salary
for those employees who earn more than 8000.

Table: employees

3. Write a query in SQL to display the first and last name, and department number for all
employees whose last name is “McEwen”.

Table: employees

4. Write a query in SQL to display all the information for all employees without any
department number.

Table: employees

5. Write a query in SQL to display all the information about the department Marketing.

Table: departments

6. Write a query in SQL to display the full name (first and last), hire date, salary, and
department number for those employees whose first name does not containing the letter M
and make the result set in ascending order by department number.

Table: employees

7. Write a query in SQL to display all the information of employees whose salary is in the
range of 8000 and 12000 and commission is not null or department number is except the
number 40, 120 and 70 and they have been hired before June 5th, 1987.
29
Table: employees

8. Write a query in SQL to display the full name (first and last name), and salary for all
employees who does not earn any commission.

Table: employees

9. Write a query in SQL to display the full name (first and last), the phone number and
email separated by hyphen, and salary, for those employees whose salary is within the
range of 9000 and 17000. The column headings assign with Full_Name, Contact_Details
and Remuneration respectively.

Table: employees

10. Write a query in SQL to display the first and last name, and salary for those employees
whose first name is ending with the letter m.

Table: employees

11. Write a query in SQL to display the full name (first and last) name, and salary, for all
employees whose salary is out of the range 7000 and 15000 and make the result set in
ascending order by the full name.

Table: employees

12. Write a query in SQL to display the full name (first and last), job id and date of hire for
those employees who was hired during November 5th, 2007 and July 5th, 2009.

Table: employees

13. Write a query in SQL to display the the full name (first and last name), and department
number for those employees who works either in department 70 or 90.

Table: employees

14. Write a query in SQL to display the full name (first and last name), salary, and manager
number for those employees who is working under a manager.

Table: employees

15. Write a query in SQL to display all the information from Employees table for those
employees who was hired before June 21st, 2002.

Table: employees

16. Write a query in SQL to display the first and last name, email, salary and manager ID,
for those employees whose managers are hold the ID 120, 103 or 145.
30
Table: employees

17. Write a query in SQL to display all the information for all employees who have the
letters D, S, or N in their first name and also arrange the result in descending order by
salary.

Table: employees

18. Write a query in SQL to display the full name (first name and last name), hire date,
commission percentage, email and telephone separated by '-', and salary for those
employees who earn the salary above 11000 or the seventh digit in their phone number
equals 3 and make the result set in a descending order by the first name.

Table: employees

19. Write a query in SQL to display the first and last name, and department number for
those employees who holds a letter s as a 3rd character in their first name.

Table : employees

20. Write a query in SQL to display the employee ID, first name, job id, and department
number for those employees who is working except the departments 50,30 and 80.

Table : employees

21. Write a query in SQL to display the employee Id, first name, job id, and department
number for those employees whose department number equals 30, 40 or 90.

Table : employees

22. Write a query in SQL to display the ID for those employees who did two or more jobs in
the past.

Table : job_history

23. Write a query in SQL to display job ID, number of employees, sum of salary, and
difference between highest salary and lowest salary for a job.

Table : employees

24. Write a query in SQL to display job ID for those jobs that were done by two or more for
more than 300 days.

Table : job_history

25. Write a query in SQL to display the country ID and number of cities in that country we
have.
31
Table : loations

26. Write a query in SQL to display the manager ID and number of employees managed by
the manager.

Table : employees

27. Write a query in SQL to display the details of jobs in descending sequence on job title.

Table : jobs

28. Write a query in SQL to display the first and last name and date of joining of the
employees who is either Sales Representative or Sales Man.

Table : employees

29. Write a query in SQL to display the average salary of employees for each department
who gets a commission percentage.

Table : employees

30. Write a query in SQL to display those departments where any manager is managing 4
or more employees.

Table : employees

31. Write a query in SQL to display those departments where more than ten employees
work who got a commission percentage.

Table : employees

32. Write a query in SQL to display the employee ID and the date on which he ended his
previous job.

Table : job_history

33. Write a query in SQL to display the details of the employees who have no commission
percentage and salary within the range 7000 to 12000 and works in that department which
number is 50.

Table : employees

34. Write a query in SQL to display the job ID for those jobs which average salary is above
8000.

Table : employees

32
35. Write a query in SQL to display job Title, the difference between minimum and
maximum salaries for those jobs which max salary within the range 12000 to 18000.

Table : jobs

36. Write a query in SQL to display all those employees whose first name or last name
starts with the letter D.

Table : employees

37. Write a query in SQL to display the details of jobs which minimum salary is greater
than 9000.

Table : jobs

38. Write a query in SQL to display those employees who joined after 7th September,
1987.

Table : employees

SQL JOINS [29 Exercises]


1. Write a SQL statement to prepare a list with salesman name, customer name and their
cities for the salesmen and customer who belongs to the same city.

Table: salesman, customer

2. Write a SQL statement to make a list with order no, purchase amount, customer name
and their cities for those orders which order amount between 500 and 2000.

Table: orders, customer

3. Write a SQL statement to know which salesman are working for which customer.

Table: customer, salesman

4. Write a SQL statement to find the list of customers who appointed a salesman for their
jobs who gets a commission from the company is more than 12%.

Table: customer, salesman

5. Write a SQL statement to find the list of customers who appointed a salesman for their
jobs who does not live in the same city where their customer lives, and gets a commission
is above 12% .

Table: customer, salesman

33
6. Write a SQL statement to find the details of a order i.e. order number, order date,
amount of order, which customer gives the order and which salesman works for that
customer and how much commission he gets for an order.

Table: orders, customer, salesman

7. Write a SQL statement to make a join on the tables salesman, customer and orders in
such a form that the same column of each table will appear once and only the relational
rows will come.

Table: orders, customer, salesman

8. Write a SQL statement to make a list in ascending order for the customer who works
either through a salesman or by own.

Table: customer, salesman

9. Write a SQL statement to make a list in ascending order for the customer who holds a
grade less than 300 and works either through a salesman or by own.

Table: customer, salesman

10. Write a SQL statement to make a report with customer name, city, order number, order
date, and order amount in ascending order according to the order date to find that either
any of the existing customers have placed no order or placed one or more orders.

Table: orders, customer

11. Write a SQL statement to make a report with customer name, city, order number, order
date, order amount salesman name and commission to find that either any of the existing
customers have placed no order or placed one or more orders by their salesman or by
own.

Table: customer, orders, salesman

12. Write a SQL statement to make a list in ascending order for the salesmen who works
either for one or more customer or not yet join under any of the customers.

Table: customer, salesman

13. Write a SQL statement to make a list for the salesmen who works either for one or
more customer or not yet join under any of the customers who placed either one or more
orders or no order to their supplier.

Table: customer, salesman, orders

34
14. Write a SQL statement to make a list for the salesmen who either work for one or more
customers or yet to join any of the customer. The customer may have placed, either one or
more orders on or above order amount 2000 and must have a grade, or he may not have
placed any order to the associated supplier.

Table: customer, salesman, orders

15. Write a SQL statement to make a report with customer name, city, order no. order
date, purchase amount for those customers from the existing list who placed one or more
orders or which order(s) have been placed by the customer who is not on the list.

Table: customer, orders

16. Write a SQL statement to make a report with customer name, city, order no. order
date, purchase amount for only those customers on the list who must have a grade and
placed one or more orders or which order(s) have been placed by the customer who is
neither in the list not have a grade.

Table: customer, orders

17. Write a SQL statement to make a cartesian product between salesman and customer
i.e. each salesman will appear for all customer and vice versa.

Table: salesman, customer

18. Write a SQL statement to make a cartesian product between salesman and customer
i.e. each salesman will appear for all customer and vice versa for that customer who
belongs to a city.

Table: salesman, customer

19. Write a SQL statement to make a cartesian product between salesman and customer
i.e. each salesman will appear for all customer and vice versa for those salesmen who
belongs to a city and the customers who must have a grade.

Table: salesman, customer

20. Write a SQL statement to make a cartesian product between salesman and customer
i.e. each salesman will appear for all customer and vice versa for those salesmen who
must belong a city which is not the same as his customer and the customers should have
an own grade.

Table: salesman, customer

21. Write a SQL query to display all the data from the item_mast, including all the data for
each item's producer company.
35
Table: company_mast, item_mast

22. Write a SQL query to display the item name, price, and company name of all the
products.

Table: company_mast, item_mast

23. Write a SQL query to display the average price of items of each company, showing the
name of the company.

Table: company_mast, item_mast

24. Write a SQL query to display the names of the company whose products have an
average price larger than or equal to Rs. 350.

Table: company_mast, item_mast

25. Write a SQL query to display the name of each company along with the ID and price
for their most expensive product.

Table: company_mast, item_mast

26. Write a query in SQL to display all the data of employees including their department.

Table: emp_department, emp_details

27. Write a query in SQL to display the first name and last name of each employee, along
with the name and sacntion amount for their department.

Table: emp_department, emp_details

28. Write a query in SQL to find the first name and last name of employees working for
departments with a budget more than Rs. 50000.

Table: emp_department, emp_details

29. Write a query in SQL to find the names of departments where more than two
employees are working.

Table: emp_department, emp_details

SQL JOINS on HR Database [27 Exercises]

1. Write a query in SQL to display the first name, last name, department number, and
department name for each employee.

36
Table: departments, employees

2. Write a query in SQL to display the first and last name, department, city, and state
province for each employee.

Table: departments, employees, locations

3. Write a query in SQL to display the first name, last name, salary, and job grade for all
employees.

Table: employees, job_grades

4. Write a query in SQL to display the first name, last name, department number and
department name, for all employees for departments 80 or 40.

Table: departments, employees

5. Write a query in SQL to display those employees who contain a letter z to their first
name and also display their last name, department, city, and state province.

Table: departments, employees, locations

6. Write a query in SQL to display all departments including those where does not have
any employee.

Table: departments, employees

7. Write a query in SQL to display the first and last name and salary for those employees
who earn less than the employee earn whose number is 182.

Table: employees

8. Write a query in SQL to display the first name of all employees including the first name
of their manager.

Table: employees

9. Write a query in SQL to display the department name, city, and state province for each
department.

Table: departments, locations

10. Write a query in SQL to display the first name, last name, department number and
name, for all employees who have or have not any department.

Table: departments, employees

37
11. Write a query in SQL to display the first name of all employees and the first name of
their manager including those who does not working under any manager.

Table: employees

12. Write a query in SQL to display the first name, last name, and department number for
those employees who works in the same department as the employee who holds the last
name as Taylor.

Table: employees

13. Write a query in SQL to display the job title, department name, full name (first and last
name ) of employee, and starting date for all the jobs which started on or after 1st January,
1993 and ending with on or before 31 August, 1997.

Table: job_history, employees, jobs

14. Write a query in SQL to display job title, full name (first and last name ) of employee,
and the difference between maximum salary for the job and salary of the employee.

Table: employees, jobs

15. Write a query in SQL to display the name of the department, average salary and
number of employees working in that department who got commission.

Table: employees, departments

16. Write a query in SQL to display the full name (first and last name ) of employee, and
job title of those employees who is working in the department which ID is 80.

Table: employees, jobs

17. Write a query in SQL to display the name of the country, city, and the departments
which are running there.

Table: countries, locations

18. Write a query in SQL to display department name and the full name (first and last
name) of the manager.

Table: departments, employees

19. Write a query in SQL to display job title and average salary of employees.

Table: employees, jobs

38
20. Write a query in SQL to display the details of jobs which was done by any of the
employees who is presently earning a salary on and above 12000.

Table: employees, job_history

21.Write a query in SQL to display the country name, city, and number of those
departments where at leaste 2 employees are working.

Table: countries, locations, employees, departments

22. Write a query in SQL to display the department name, full name (first and last name) of
manager, and their city.

Table: employees, departments

23. Write a query in SQL to display the employee ID, job name, number of days worked in
for all those jobs in department 80.

Table: jobs, job_history

24. Write a query in SQL to display the full name (first and last name), and salary of those
employees who working in any department located in London.

Table: departments, locations, employees

25. Write a query in SQL to display full name(first and last name), job title, starting and
ending date of last jobs for those employees with worked without a commission
percentage.

Table: jobs, job_history, employees

26. Write a query in SQL to display the department name and number of employees in
each of the department.

Table: departments, employees

27. Write a query in SQL to display the full name (firt and last name ) of employee with ID
and name of the country presently where (s)he is working.

Table: countries, locations, employees, departments

SQL SUBQUERIES [39 Exercises]


1. Write a query to display all the orders from the orders table issued by the salesman
'Paul Adam'.

Table: Salesman, Orders

39
2. Write a query to display all the orders for the salesman who belongs to the city London.

Table: Salesman, Orders

3. Write a query to find all the orders issued against the salesman who works for customer
whose id is 3007.

Table: Salesman, Orders

4. Write a query to display all the orders which values are greater than the average order
value for 10th October 2012.

Table: Salesman, Orders

5. Write a query to find all orders attributed to a salesman in New york.

Table: Salesman, Orders

6. Write a query to display the commission of all the salesmen servicing customers in
Paris.

Table: Salesman, Orders

7. Write a query to display all the customers whose id is 2001 bellow the salesman ID of
Mc Lyon.

Table: Salesman, Orders

8. Write a query to counts the customers with grades above New York's average.

Table: Salesman, Customer, Orders

9. Write a query to display all customers with orders on October 5, 2012.

Table: Orders

10. Write a query to display all the customers with orders issued on date 17th August,
2012.

Table: Orders, Customer

11. Write a query to find the name and numbers of all salesmen who had more than one
customer.

Table: Customer, Salesman

12. Write a query to find all orders with order amounts which are above-average amounts
for their customers.
40
Table: Orders, Customer

13. Write a queries to find all orders with order amounts which are on or above-average
amounts for their customers.

Table: Orders, Customer

14. Write a query to find the sums of the amounts from the orders table, grouped by date,
eliminating all those dates where the sum was not at least 1000.00 above the maximum
order amount for that date.

Table: Orders, Customer

15. Write a query to extract the data from the customer table if and only if one or more of
the customers in the customer table are located in London.

Table: Customer

16. Write a query to find the salesmen who have multiple customers.

Table: Customer

17. Write a query to find all the salesmen who worked for only one customer.

Table: Customer, Salesman

18. Write a query that extract the rows of all salesmen who have customers with more than
one orders.

Table: Salesman, Orders, Customer

19. Write a query to find salesmen with all information who lives in the city where any of
the customers lives.

Table: Salesman, customer

20. Write a query to find all the salesmen for whom there are customers that follow them.

Table: Salesman, customer

21. Write a query to display the salesmen which name are alphabetically lower than the
name of the customers.

Table: Salesman, Customer

22. Write a query to display the customers who have a greater gradation than any
customer who belongs to the alphabetically lower than the city New York.
41
Table: Salesman, Customer

23. Write a query to display all the orders that had amounts that were greater than at least
one of the orders on October 9th 2012.

Table: Orders

24. Write a query to find all orders with an amount smaller than any amount for a customer
in London.

Table: Salesman, Customer

25. Write a query to display all orders with an amount smaller than any amount for a
customer in London.

Table: Salesman, Customer

26. Write a query to display only those customers whose grade are, in fact, higher than
every customer in New York.

Table: Salesman, Customer

27. Write a query to find only those customers whose grade are, higher than every
customer to the city New York.

Table: Salesman, Customer

28. Write a query to get all the information for those customers whose grade is not as the
grade of customer who belongs to the city London.

Table: Salesman, Customer

29. Write a query to find all those customers whose grade are not as the grade, belongs to
the city Paris.

Table: Salesman, Customer

30. Write a query to find all those customers who hold a different grade than any customer
of the city Dallas.

Table: Salesman, Customer

31. Write a SQL query to find the average price of each manufacturer's products along with
their name.

Table: company_mast, item_mast

42
32. Write a SQL query to display the average price of the products which is more than or
equal to 350 along with theri names.

Table: company_mast, item_mast

33. Write a SQL query to display the name of each company, price for their most
expensive product along with their ID.

Table: company_mast, item_mast

34. Write a query in SQL to find all the details of employees whose last name is Gabriel or
Dosio.

Table: emp_department, emp_details

35. Write a query in SQL to display all the details of employees who works in department
89 or 63.

Table: emp_department, emp_details

36. Write a query in SQL to display the first name and last name of employees working for
the department which allotment amount is more than Rs.50000.

Table: emp_department, emp_details

37. Write a query in SQL to find the departments which sanction amount is larger than the
average sanction amount of all the departments.

Table: emp_department, emp_details

38. Write a query in SQL to find the names of departments with more than two employees
are working.

Table: emp_department, emp_details

39. Write a query in SQL to find the first name and last name of employees working for
departments which sanction amount is second lowest.

Table: emp_department, emp_details

SQL SUBQUERIES on HR Database [55 Exercises]

1. Write a query to display the name ( first name and last name ) for those employees who
gets more salary than the employee whose ID is 163.

Table: employees
43
2. Write a query to display the name ( first name and last name ), salary, department id, job
id for those employees who works in the same designation as the employee works whose
id is 169.

Table: employees

3. Write a query to display the name ( first name and last name ), salary, department id for
those employees who earn such amount of salary which is the smallest salary of any of the
departments.

Table: employees

4. Write a query to display the employee id, employee name (first name and last name ) for
all employees who earn more than the average salary.

Table: employees

5. Write a query to display the employee name ( first name and last name ), employee id
and salary of all employees who report to Payam.

Table: employees

6. Write a query to display the department number, name ( first name and last name ), job
and department name for all employees in the Finance department.

Table: employees, departments

7. Write a query to display all the information of an employee whose salary and reporting
person id is 3000 and 121 respectively.

Table : employees

8. Display all the information of an employee whose id is any of the number 134, 159 and
183.

Table: employees

9. Write a query to display all the information of the employees whose salary is within the
range 1000 and 3000.

Table: employees

10. Write a query to display all the information of the employees whose salary is within the
range of smallest salary and 2500.

Table: employees

44
11. Write a query to display all the information of the employees who does not work in
those departments where some employees works whose id within the range 100 and 200.

Table: employees, departments

12. Write a query to display all the information for those employees whose id is any id who
earn the second highest salary.

Table: employees

13. Write a query to display the employee name( first name and last name ) and hiredate
for all employees in the same department as Clara. Exclude Clara.

Table: employees

14. Write a query to display the employee number and name( first name and last name )
for all employees who work in a department with any employee whose name contains a
T.

Table: employees

15. Write a query to display the employee number, name( first name and last name ), and
salary for all employees who earn more than the average salary and who work in a
department with any employee with a J in their name.

Table: employees

16. Display the employee name( first name and last name ), employee id, and job title for
all employees whose department location is Toronto.

Table: employees, departments

17. Write a query to display the employee number, name( first name and last name ) and
job title for all employees whose salary is smaller than any salary of those employees
whose job title is MK_MAN.

Table: employees

18. Write a query to display the employee number, name( first name and last name ) and
job title for all employees whose salary is smaller than any salary of those employees
whose job title is MK_MAN. Exclude Job title MK_MAN.

Table: employees

45
19. Write a query to display the employee number, name( first name and last name ) and
job title for all employees whose salary is more than any salary of those employees whose
job title is PU_MAN. Exclude job title PU_MAN.

Table: employees

20. Write a query to display the employee number, name( first name and last name ) and
job title for all employees whose salary is more than any average salary of any
department.

Table: employees

21. Write a query to display the employee name( first name and last name ) and
department for all employees for any existence of those employees whose salary is more
than 3700.

Table: employees

22. Write a query to display the department id and the total salary for those departments
which contains at least one salaried employee.

Table: employees, departments

23. Write a query to display the employee id, name ( first name and last name ) and the job
id column with a modified title SALESMAN for those employees whose job title is ST_MAN
and DEVELOPER for whose job title is IT_PROG.

Table: employees

24. Write a query to display the employee id, name ( first name and last name ), salary and
the SalaryStatus column with a title HIGH and LOW respectively for those employees
whose salary is more than and less than the average salary of all employees.

Table: employees

25. Write a query to display the employee id, name ( first name and last name ),
SalaryDrawn, AvgCompare (salary - the average salary of all employees) and the
SalaryStatus column with a title HIGH and LOW respectively for those employees whose
salary is more than and less than the average salary of all employees.

Table: employees

26. Write a subquery that returns a set of rows to find all departments that do actually have
one or more employees assigned to them.

Table: employees, departments


46
27. Write a query that will identify all employees who work in departments located in the
United Kingdom.

Table: employees, departments, locations, countries

28. Write a query to identify all the employees who earn more than the average and who
work in any of the IT departments.

Table: employees, departments

29. Write a query to determine who earns more than Mr. Ozer.

Table: employees

30. Write a query to find out which employees have a manager who works for a
department based in the US.

Table: employees, departments, locations

31. Write a query which is looking for the names of all employees whose salary is greater
than 50% of their department’s total salary bill.

Table : employees

32. Write a query to get the details of employees who are managers.

Table: employees, departments

33. Write a query to get the details of employees who manage a department.

Table: employees, departments

34. Write a query to display the employee id, name ( first name and last name ), salary,
department name and city for all the employees who gets the salary as the salary earn by
the employee which is maximum within the joining person January 1st, 2002 and
December 31st, 2003.

Table: employees, departments, locations

35. Write a query in SQL to display the department code and name for all departments
which located in the city London.

Table: departments, locations

36. Write a query in SQL to display the first and last name, salary, and department ID for
all those employees who earn more than the average salary and arrange the list in
descending order on salary.
47
Table: employees

37. Write a query in SQL to display the first and last name, salary, and department ID for
those employees who earn more than the maximum salary of a department which ID is
40.

Table: employees

38. Write a query in SQL to display the department name and Id for all departments where
they located, that Id is equal to the Id for the location where department number 30 is
located.

Table: departments

39. Write a query in SQL to display the first and last name, salary, and department ID for
all those employees who work in that department where the employee works who hold the
ID 201.

Table: employees

40. Write a query in SQL to display the first and last name, salary, and department ID for
those employees whose salary is equal to the salary of the employee who works in that
department which ID is 40.

Table: employees

41. Write a query in SQL to display the first and last name, and department code for all
employees who work in the department Marketing.

Table: employees, departments

42.Write a query in SQL to display the first and last name, salary, and department ID for
those employees who earn more than the minimum salary of a department which ID is
40.

Table: employees

43. Write a query in SQL to display the full name,email, and designation for all those
employees who was hired after the employee whose ID is 165.

Table: employees

44. Write a query in SQL to display the first and last name, salary, and department ID for
those employees who earn less than the minimum salary of a department which ID is 70.

Table: employees
48
45. Write a query in SQL to display the first and last name, salary, and department ID for
those employees who earn less than the average salary, and also work at the department
where the employee Laura is working as a first name holder.

Table: employees

46. Write a query in SQL to display the first and last name, salary and department ID for
those employees whose department is located in the city London.

Table: employees

47. Write a query in SQL to display the city of the employee whose ID 134 and works
there.

Table: locations, departments, employees

48. Write a query in SQL to display the the details of those departments which max salary
is 7000 or above for those employees who already done one or more jobs.

Table: departments, employees, job_history

49. Write a query in SQL to display the detail information of those departments which
starting salary is at least 8000.

Table: departments, employees

50. Write a query in SQL to display the full name (first and last name) of manager who is
supervising 4 or more employees.

Table : employees

51. Write a query in SQL to display the details of the current job for those employees who
worked as a Sales Representative in the past.

Table: jobs, employees, job_history

52. Write a query in SQL to display all the infromation about those employees who earn
second lowest salary of all the employees.

Table : employees

53. Write a query in SQL to display the details of departments managed by Susan.

Table: departments, employees

54. Write a query in SQL to display the department ID, full name (first and last name),
salary for those employees who is highest salary drawar in a department.
49
Table: employees

55. Write a query in SQL to display all the information of those employees who did not
have any job in the past.

Table: employees, job_history

SQL Union[9 Exercises]


1. Write a query to display all salesmen and customer located in London.

Table: Salesman, Customer

2. Write a query to display distinct salesman and their cities.

Table: Salesman, Customer

3. Write a query to display all the salesmen and customer involved in this inventory
management system.

Table: orders, customer

4. Write a query to make a report of which salesman produce the largest and smallest
orders on each date.

Table: Salesman, Customer, Orders

5. Write a query to make a report of which salesman produce the largest and smallest
orders on each date and arranged the orders number in smallest to the largest number.

Table: Salesman, Customer

6. Write a query to list all the salesmen, and indicate those who do not have customers in
their cities, as well as whose who do.

Table: Salesman, Customer

7. Write a query to that appends strings to the selected fields, indicating whether or not a
specified salesman was matched to a customer in his city.

Table: Salesman, Customer

8. Create a union of two queries that shows the names, cities, and ratings of all customers.
Those with a rating of 200 or greater will also have the words "High Rating", while the
others will have the words "Low Rating".

Table: Customer

50
9. Write a command that produces the name and number of each salesman and each
customer with more than one current order. Put the results in alphabetical order.

Table: Customer, salesman, orders

SQL View [16 Exercises]

1. Write a query to create a view for those salesmen belongs to the city New York.

Table: salesman

2. Write a query to create a view for all salesmen with columns salesman_id, name, and
city.

Table: salesman

3. Write a query to find the salesmen of the city New York who achieved the commission
more than 13%.

Table: salesman

4. Write a query to create a view to getting a count of how many customers we have at
each level of a grade.

Table: customer, customer

5. Write a query to create a view to keeping track the number of customers ordering,
number of salesmen attached, average amount of orders and the total amount of orders in
a day.

Table : orders

6. Write a query to create a view that shows for each order the salesman and customer by
name.

Table: salesman, customer, orders

7. Write a query to create a view that finds the salesman who has the customer with the
highest order of a day.

Table: salesman, orders

8. Write a query to create a view that finds the salesman who has the customer with the
highest order at least 3 times on a day.
51
Table: customer, salesman

9. Write a query to create a view that shows all of the customers who have the highest
grade.

Table: customer

10. Write a query to create a view that shows the number of the salesman in each city.

Table: salesman

11. Write a query to create a view that shows the average and total orders for each
salesman after his or her name. (Assume all names are unique)

Table: salesman, orders

12. Write a query to create a view that shows each salesman with more than one
customers.

Table: salesman, customer

13. Write a query to create a view that shows all matches of customers with salesman
such that at least one customer in the city of customer served by a salesman in the city of
the salesman.

Table: salesman, customer

14. Write a query to create a view that shows the number of orders in each day.

Table: orders

15. Write a query to create a view that finds the salesmen who issued orders on October
10th, 2012.

Table: salesman, orders

16. Write a query to create a view that finds the salesmen who issued orders on either
August 17th, 2012 or October 10th, 2012.

Table: orders

SQL User Account Management [16 Exercise]


1. How to create a user on localhost.

Syntax :
create user [user name]@[<localhost>|<IP address>|<any host('%')>]

52
identified by ["password"];
Example :
create user ramaswamy@localhost identified by "mypasword";

 'ramaswamy' is the user name


 'localhost' is the machine name, here same machine
 'mypassword' is the password

2. How to create a user for an IP address other than localhost.

Syntax :
create user [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :
create user ramaswamy@192.168.0.105 identified by "mypasword";

 'ramaswamy' is the user name


 '192.168.0.105' is the IP address
 'mypassword' is the password

3. How to grant permission to a user to select only from localhost.

Syntax :
grant [<permission_1,permission_2,...permission_n>|<all>]
on [database].[<table_name>|<all_tables(*)>]
to [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :
grant select on posts.* to ramaswamy@localhost identified by 'mypassword';
flush privileges;

 'select' is permission name


 'posts' is the database name
 '*' is used for all tables in the database
 'ramaswamy' is the user name
 'localhost' is the machine name, here same machine
 'mypassword' is the password

4. How to grant a user permission to create, insert, update, delete and create temporary
tables from localhost.

Syntax :
grant [<permission_1,permission_2,...permission_n>|<all>]
53
on [database].[<table_name>|<all_tables(*)>]
to [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :
grant select, create, insert, update, delete, create temporary tables
on posts.* to amit@localhost identified by 'mypassword';
flush privileges;

 'select','create','insert','update','delete','create temporary tables' are the permission name


 'posts' is the database name
 '*' is used for all tables in the database
 'amit' is the user name
 'localhost' is the machine name, here same machine
 'mypassword' is the password

5. How to grant a user permission to create, insert, update, delete and create temporary
tables from any host.

Syntax :
grant [<permission_1,permission_2,...permission_n>|<all>]
on [database].[<table_name>|<all_tables(*)>]
to [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :
grant select, create, insert, update, delete, create temporary tables on posts.*
to amit@'%' identified by 'mypassword';
flush privileges;

 'select','create','insert','update','delete','create temporary tables' are the permission name


 'posts' is the database name
 '*' is used for all tables in the database
 'amit' is the user name
 '%' is used for any host
 'mypassword' is the password

6. How to grant a user permission to select only from any host but to a specific table of a
database.

Syntax :
grant [<permission_1,permission_2,...permission_n>|<all>]
on [database].[<table_name>|<all_tables(*)>]
to [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :

54
grant select on posts.url_master to jhon@'%' identified by 'mypassword';
flush privileges;

 'select' is the permission name


 'posts' is the database name
 'url_master' is the table name in the database 'posts'
 'jhon' is the user name
 '%' is used for any host
 'mypassword' is the password

7. How to grant all privileges to a user from all machines.

Syntax :
grant [<permission_1,permission_2,...permission_n>|<all>]
on [database].[<table_name>|<all_tables(*)>]
to [user name]@[<localhost>|<IP address>|<any host('%')>]
identified by ["password"];
Example :
grant all on posts.* to joy@'%' identified by 'mypassword';
flush privileges;

 'all' is used for all the permission


 'posts' is the database name
 '*' is used for all tables in the database
 'joy' is the user name
 '%' is used for any host
 'mypassword' is the password

8. How to revoke all privileges from a user.

Syntax :
revoke [<permission_1,permission_2,...permission_n>|<all privileges>]
on [database].[<table_name>|<all_tables(*)>]
from [user name]@[<localhost>|<IP address>|<any host('%')>];
Example :
revoke all privileges on posts.* from joy@'%';
flush privileges;

 'all privileges' is used for all the permission


 'posts' is the database name
 '*' is used for all tables in the database
 'joy' is the user name
 '%' is used for any host

55
9. How to revoke specific privilege from a user.

Syntax :
revoke [<permission_1,permission_2,...permission_n>|<all privileges>]
on [database].[<table_name>|<all_tables(*)>]
from [user name]@[<localhost>|<IP address>|<any host('%')>];
Example :
revoke select on posts.* from joy@'%';
flush privileges;

 'select' is permission name


 'posts' is the database name
 '*' is used for all tables in the database
 'joy' is the user name
 '%' is used for any host

10. How to check permissions granted to a specific user.

Example :
show grants for amit;
11. How to check the list of system privileges that the MySQL server supports.

Example :
show privileges;
12. How to Grant permission to a user so that (s)he can execute not more than a specific
number of queries in an hour.

Example :
create user steffi@localhost identified by 'mypassword';
grant select on posts.* to steffi@localhost
identified by 'mypassword' with MAX_QUERIES_PER_HOUR 50;
flush privileges;
13. How to Grant permission to a user so that (s)he can execute not more than a specific
number of queries in an hour.

Example :
set password for steffi@localhost = password('mypassword123');
14. How to delete user.

Example :
drop user steffi@'localhost';
15. How to rename a user.

56
Example :
rename user amit@localhost to sumit@localhost;
16. How to create a user and granting no privileges

Example :
grant usage on posts.* to boris@localhost identified by 'mypassword';
Note : Command shown as solution is executed successfully on MySQL Server 5.6

57

Das könnte Ihnen auch gefallen