DEV Community

Aruna Arun
Aruna Arun

Posted on

Day 3: SQL

SELECT Statement & DISTINCT Clause

1. What is SELECT Statement?
The SELECT statement is used to retrieve data from a table.
It is the most important and frequently used SQL command.

2. Basic SELECT Syntax
SELECT column_name
FROM table_name;

3. Select All Columns (*)
Fetches all columns from a table.
SELECT * FROM employees;
* means all columns

4. Select Specific Columns
Retrieve only required columns.
SELECT emp_name, salary
FROM employees;

5. Column Aliases (AS)
Used to give temporary names to columns in output.
SELECT emp_name AS Name, salary AS Salary
FROM employees;
Alias without AS:
SELECT emp_name Name, salary Salary
FROM employees;

6. SELECT with Expressions
You can perform calculations inside SELECT.
SELECT emp_name, salary * 12 AS Annual_Salary
FROM employees;

7. DISTINCT Keyword
Used to remove duplicate values from results.
Example Table: employees

emp_id emp_name department
1 Ravi IT
2 Priya HR
3 Karthik IT
4 Anu HR

Without DISTINCT:
SELECT department FROM employees;
Result:
IT
HR
IT
HR

With DISTINCT:
SELECT DISTINCT department FROM employees;
Result:
IT
HR

8. DISTINCT with Multiple Columns
Distinct combination of columns.
SELECT DISTINCT department, emp_name
FROM employees;

9. SELECT with ORDER BY (Intro)
SELECT emp_name, salary
FROM employees
ORDER BY salary;
Descending order:
ORDER BY salary DESC;

Top comments (1)

Collapse
 
shahrouzlogs profile image
Shahrouz Nikseresht

Good luck 🚀