More SQL Queries
CREATE TABLE Worker ( WORKER_ID INT NOT NULL PRIMARY KEY
AUTO_INCREMENT, FIRST_NAME CHAR(25), LAST_NAME CHAR(25),
SALARY INT(15), JOINING_DATE DATETIME, DEPARTMENT CHAR(25) );
CREATE TABLE Bonus (WORKER_REF_ID INT, BONUS_AMOUNT INT(10),
BONUS_DATE DATETIME, FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID) ON DELETE CASCADE );
CREATE TABLE Title (WORKER_REF_ID INT, WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME, FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID) ON DELETE CASCADE );
1.Write an SQL query to get “FIRST_NAME” from Worker table using the alias name as
<WORKER_NAME>
Select FIRST_NAME AS WORKER_NAME from Worker;
2.Write an SQL query to get unique values of DEPARTMENT from Worker table.
Select distinct DEPARTMENT from Worker;
3.Write an SQL query to print the FIRST_NAME and LAST_NAME from Worker table into
a single column COMPLETE_NAME. A space char should separate them.
Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;
4.Write an SQL query to get all Worker details from the Worker
table order by FIRST_NAME Ascending.
Select * from Worker order by FIRST_NAME asc;
5.Write an SQL query to get all Worker details from the Worker
table order by FIRST_NAME Ascending and DEPARTMENT
Descending.
Select * from Worker order by FIRST_NAME asc, DEPARTMENT
desc;
6.Write an SQL query to get the details for Workers with the
first name as “Vipul” and “Satish” from Worker table.
Select * from Worker where FIRST_NAME in ('Vipul','Satish');
4.Write an SQL query to get all Worker details from the Worker
table order by FIRST_NAME Ascending.
Select * from Worker order by FIRST_NAME asc;
5.Write an SQL query to get all Worker details from the Worker
table order by FIRST_NAME Ascending and DEPARTMENT
Descending.
Select * from Worker order by FIRST_NAME asc, DEPARTMENT
desc;
6.Write an SQL query to get the details for Workers with the
first name as “Vipul” and “Satish” from Worker table.
Select * from Worker where FIRST_NAME in ('Vipul','Satish');
7. Write an SQL query to get the details of workers excluding
first names, “Vipul” and “Satish” from Worker table.
Select * from Worker where FIRST_NAME not in
('Vipul','Satish');
The SQL LIKE Operator:
The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
There are two wildcards often used in conjunction with the
LIKE operator:
• The percent sign (%) represents zero, one, or multiple
characters
• The underscore sign (_) represents one, single character
• Here are some examples showing different LIKE operators
with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and
are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and
are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and
ends with "o"
8. Write an SQL query to print details of Workers with
DEPARTMENT name as “Admin”.
Select * from Worker where DEPARTMENT like 'Admin%';
9. Write an SQL query to print details of the Workers whose
FIRST_NAME contains ‘a’.
Select * from Worker where FIRST_NAME like '%a%';
10. Write an SQL query to print details of the Workers whose
FIRST_NAME ends with ‘a’.
Select * from Worker where FIRST_NAME like '%a';
11. Write an SQL query to print details of the Workers whose
FIRST_NAME starts with ‘a’.
Select * from Worker where FIRST_NAME like 'a%';
12. Write an SQL query to print details of the Workers whose
FIRST_NAME ends with ‘h’ and contains six alphabets.
Select * from Worker where FIRST_NAME like '_____h';
13. The following SQL statement selects all customers with a
CustomerName that have "or" in any position:
SELECT * FROM Customers WHERE CustomerName LIKE '%or
%';
14. The following SQL statement selects all customers with a
CustomerName that have "r" in the second position:
SELECT * FROM Customers WHERE CustomerName LIKE '_r
%';
15. The following SQL statement selects all customers with a
CustomerName that starts with "a" and are at least 3
characters in length:
SELECT * FROM Customers WHERE CustomerName LIKE 'a__
%';
16. The following SQL statement selects all customers with a
ContactName that starts with "a" and ends with "o":
SELECT * FROM Customers WHERE ContactName LIKE 'a%o';
17. The following SQL statement selects all customers with a
CustomerName that does NOT start with "a":
SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%';
18. Write an SQL query to print details of the Workers whose SALARY
lies between 100000 and 500000.
Select * from Worker where SALARY between 100000 and 500000;
19. Write an SQL query to fetch worker names with salaries >= 50000 and
<= 100000.
SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Worker_Name,
Salary FROM worker WHERE WORKER_ID IN (SELECT WORKER_ID
FROM worker WHERE Salary BETWEEN 50000 AND 100000);
20. Write an SQL query to fetch the count of employees working in the
department ‘Admin’.
SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';
21. Write an SQL query to fetch the no. of workers for each department in
the descending order.
SELECT DEPARTMENT, count(WORKER_ID) as No_Of_Workers
FROM worker GROUP BY DEPARTMENT ORDER BY No_Of_Workers
DESC;
22. Write an SQL query to show only odd rows from a table.
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
23. Write an SQL query to show only even rows from a table.
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;
24. Write an SQL query to show the current date and time.
Following MySQL query returns the current date:
SELECT CURDATE();
Following MySQL query returns the current date and time:
SELECT NOW();
Following MySQL query returns the current date and time:
SELECT sysdate();
25. The UNION Operator:
Example 1:
SELECT City FROM Customer
UNION
SELECT City FROM Supplier
ORDER BY City;
Example 2:
SELECT City, Country FROM Customer
WHERE Country='India'
UNION
SELECT City, Country FROM Supplier
WHERE Country='India'
ORDER BY City;
26. INTERSECT Operator
SELECT City, Country FROM Customer
WHERE customerid>=1002
intersect
SELECT City, Country FROM Supplier
WHERE supplierid>=2
ORDER BY City;
27. Except or minus operator:
SELECT City, Country FROM Customer
WHERE customerid>=1002
except
SELECT City, Country FROM Supplier
WHERE supplierid>=2
ORDER BY City;
28. Write an SQL query to show the first 3 records of a table.
SELECT * FROM Customers LIMIT 3;
29. Write an SQL query to show the top n (say 10) records of a table.
Following MySQL query will return the top n records using the LIMIT
method:
SELECT * FROM Worker ORDER BY Salary DESC LIMIT 10;
30. Write an SQL query to show maximum salary
select max(salary) from worker
31. Write an SQL query to determine the nth highest salary from a table.
SELECT salary FROM employee ORDER BY salary DESC LIMIT n-1,1
If n=1(Maximum),
SELECT salary FROM employee ORDER BY salary DESC LIMIT 1
If n=2 (2nd highest),
SELECT salary FROM employee ORDER BY salary DESC LIMIT 1,1
If n=3 (3rd highest),
SELECT salary FROM employee ORDER BY salary DESC LIMIT 2,1
32. Write an SQL query to determine the nth highest salary from a table.
SELECT salary FROM employee ORDER BY salary ASC LIMIT n-1,1
If n=1(Minimum),
SELECT salary FROM employee ORDER BY salary ASC LIMIT 1
If n=2 (2nd lowest),
Sql: SELECT salary FROM employee ORDER BY salary ASC LIMIT 1,1
If n=3 (3rd lowest),
SELECT salary FROM employee ORDER BY salary ASC LIMIT 2,1
33. SQL update and arithmetic Operation
Update customers Set salary = salary + 1000 Where id = 7;
34. Write an SQL query to get the departments that have less than five
people in it.
SELECT DEPARTMENT, COUNT(WORKER_ID) as 'Number of Workers'
FROM Worker GROUP BY DEPARTMENT HAVING
COUNT(WORKER_ID) < 5;
35. Write an SQL query to show all departments along with the
number of people in there.
SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of
Workers' FROM Worker GROUP BY DEPARTMENT;
36. Write an SQL query to get departments along with the total
salaries paid for each of them.
SELECT DEPARTMENT, sum(Salary) from worker group by
DEPARTMENT;
37. Write an SQL query to get the names of workers who earn
the highest salary.
SELECT FIRST_NAME, SALARY from Worker WHERE
SALARY=(SELECT max(SALARY) from Worker);