SELECT Statements

Estimated reading: 2 minutes 20 views

1. Write a query to fetch specific columns from a table

To fetch specific columns, use the SELECT statement followed by the column names you need.
Example:

				
					SELECT name, salary FROM employees;

				
			

2. How do you retrieve all rows and columns from a table?

Use SELECT * to fetch all rows and columns from the table.
Example:

				
					SELECT * FROM employees;

				
			

3. What is the use of aliases (AS keyword) in a SELECT statement? Provide an example.

Aliases are used to give columns or tables a temporary name for better readability or convenience.
Example:

				
					SELECT name AS EmployeeName, salary AS EmployeeSalary FROM employees;

				
			

4. How do you use mathematical expressions or calculated fields in a SELECT query?

You can use arithmetic expressions like addition, subtraction, multiplication, and division to calculate new fields.
Example:

				
					SELECT name, salary * 12 AS AnnualSalary FROM employees;

				
			

5. Write a query to combine columns from multiple tables using a JOIN

Use the JOIN clause to combine columns from multiple tables based on a related column.
Example:

				
					SELECT e.name, d.department_name 
FROM employees e 
JOIN departments d 
ON e.department_id = d.id;

				
			

6. How can you fetch the first N rows from a table using SQL?

In Oracle, you can use ROWNUM to limit the number of rows returned.
Example:

				
					SELECT * FROM employees WHERE ROWNUM <= 5;

				
			

Leave a Comment

Share this Doc

SELECT Statements

Or copy link

CONTENTS