BASIC SQL Query


Q.1 Write an SQL query for fetching “FIRST_NAME” from the Customer table using < Customer_NAME> as alias.

Ans. The query that you can use is:

Select FIRST_NAME AS Customer_NAME from Customer;

Q.2 What is an SQL Query for fetching the “FIRST_NAME” from Customer table in upper case?

Ans. The query that you can use is:

Select upper(FIRST_NAME) from Customer;

Q.3 What is an SQL query for fetching the unique values of the column DEPARTMENT from the Customer table?

Ans. The query that you cam use is:

Select distinct DEPARTMENT from Customer;

Q.4 Write an SQL query for printing the first three characters of the column FIRST_NAME.

Ans. The query that can be used is:

Select substring(FIRST_NAME,1,3) from Customer;

Q.5 What is an SQL query for finding the position of the alphabet (‘A’) in the FIRST_NAME column of Ayush.

Ans. The query that can be used is:

Select INSTR(FIRST_NAME, BINARY'a') from Customer where FIRST_NAME = 'Ayush';

Q.6 What is an SQL Query for printing the FIRST_NAME from Customer Table after the removal of white spaces from right side.

Ans. The query that can be used is:

Select RTRIM(FIRST_NAME) from Customer;

Q.7 Write an SQL Query for printing the DEPARTMENT from Customer Table after the removal of white spaces from the left side.

Ans. The query that you can use is:

Select LTRIM(DEPARTMENT) from Customer;

Q.8 What is an SQL query for fetching the unique values from the DEPARTMENT column and thus printing is the length?

Ans. The query that you can use is:

Select distinct length(DEPARTMENT) from Customer;

Q.9 Write an SQL query for printing the FIRST_NAME after replacing ‘A’ with ‘a’.

The query that can be used is:

Select REPLACE(FIRST_NAME,'a','A') from Customer;

Q.10 What is an SQL query for printing the FIRST_NAME and LAST_NAME into a column named COMPLETE_NAME? (A space char should be used)

Ans. The query that can be used is:

Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Customer;

Q.11 What is an SQL query for printing all details of the Customer table which ordered by FIRST_NAME ascending?

Ans. The query that can be used is:

Select * from Customer order by FIRST_NAME asc;

Q.12 Write an SQL query for printing the all details of the Customer table which ordered by FIRST_NAME ascending and the DEPARTMENT in descending 

The query that can be used is:

Select * from Customer order by FIRST_NAME asc,DEPARTMENT desc;

Q.13 What is an SQL query to print the details of the Customer ‘NISHANT’ and ‘PRIYANSH’.

Ans. The query that can be used is:

Select * from Customer where FIRST_NAME in ('NISHANT','PRIYANSH');

Q.14 What is an SQL query printing all details of Customer excluding the first names of  ‘NISHANT’ and ‘PRIYANSH’.

Ans. The query that can be used is:

Select * from Customer where FIRST_NAME not in ('NISHANT','PRYIANSH');

Q.15 Write an SQL query for printing the details of DEPARTMENT name as “Admin”.

Ans. The query that can be used is:

Select * from Customer where DEPARTMENT like 'Admin%';

Q.16  What is an SQL query for printing the details of Customer whose FIRST_NAME Contains ‘A’?

Ans. The query that can be used is:

Select * from Customer where FIRST_NAME like '%a%';

Q.17 What is an SQL Query for printing the FIRST_NAME of Customer whose name ends with ‘A’?

Ans. The query that can be used is:

Select * from Customer where FIRST_NAME like '%a';

Q.18 What is an SQL Query for printing the details of the Customer whose FIRST_NAME ends with ‘H’ and contains six alphabets?

Ans. The query that can be used is:

Select * from Customer where FIRST_NAME like '_____h';

Q.19 Write an SQL Query for printing the details of Customer whose SALARY lies between 10000 and 20000.

Ans. The query that can be used is:

Select * from Customer where SALARY between 10000 and 20000;

Q.20 Write an SQL Query for printing the details of Customer who joined in Feb’2014

Ans. The query that can be used is:

Select * from Customer where year(JOINING_DATE) = 2014 and month(JOINING_DATE) = 2;

Q.21 Write an SQL Query for fetching the count of Customer in DEPARTMENT with ‘Admin’.

Ans. The query that can be used is:

SELECT COUNT(*) FROM Customer WHERE DEPARTMENT = 'Admin';

Q.22 Write an SQL Query for fetching the details of Customer with Salaries >= 5000 and <= 10000.

Ans. The query that can be used is:

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Customer_Name, Salary
FROM Customer
WHERE Customer_ID IN
(SELECT Customer_ID FROM Customer
WHERE Salary BETWEEN 5000 AND 10000);

Q.23 What is an SQL Query for fetching the no. of Customer in each department in descending order?

Ans. The query that can be used is:

SELECT DEPARTMENT, count(Customer_ID) No_Of_Customer
FROM Customer
GROUP BY DEPARTMENT
ORDER BY No_Of_Customer DESC;

Q.24 What is an SQL Query for printing the details of Customer who are also managers?

Ans. The query that can be used is:

SELECT DISTINCT W.FIRST_NAME, T.Customer_TITLE
FROM Customer W
INNER JOIN Title T
ON W.Customer_ID = T.Customer_REF_ID
AND T.Customer_TITLE in ('Manager');

Q.25 Write an SQL Query for fetching the details of duplicate records in some fields.

Ans. The query that can be used is:

SELECT Customer_TITLE, AFFECTED_FROM, COUNT(*)
FROM Title
GROUP BY Customer_TITLE, AFFECTED_FROM
HAVING COUNT(*) > 1;

Q.26 What is an SQL Query for only showing odd rows?

Ans. The query that can be used is:

SELECT * FROM Customer WHERE MOD (Customer_ID, 2) <> 0;

Q.27 What is an SQL Query for only showing even rows?

Ans. The query that can be used is:

SELECT * FROM Customer WHERE MOD (Customer_ID, 2) = 0;

Q.28 Write an SQL Query for cloning a new table from another table.

Ans. The general query that can be used to clone a table with data is:

SELECT * INTO CustomerClone FROM Customer;

Q.29 Write an SQL Query for fetching the intersecting details of two tables.

Ans. The query that can be used is:

(SELECT * FROM Customer)
INTERSECT
(SELECT * FROM CustomerClone);

Q.30 What is an SQL Query for showing the details of one table that another doesn’t have.

Ans. The query that can be used is:

SELECT * FROM Customer
MINUS
SELECT * FROM Title;

Q.31 Write a query to count the number of unique records in a table?

We can count the number of unique records in our table using the Distinct and the count keywords.

Query:

SELECT COUNT(distinct(emp_id)) as noOfUniqueRecords FROM Customer;

Q.32 Write a query to show the working of the SUM() function in SQL?

The SUM() function in SQL is put to use on the integer columns to return the total of all the entries.

Let us write the query to understand it further.

Query:

SELECT SUM(experience) as Total_Experience FROM Customer;


SQL JOINS

No comments:

Post a Comment

Stay Connected To Get Free Updates!

Subscribe via Email

You can also receive Free Email Updates:

Widget by NBT