SQL
The SELECT statement is used to select data from a database.
SELECT retrieves rows, columns and derived values from one or more tables.
SYNTAX:
SELECT * FROM table_name;
SELECT ID,NAME FROM table_name;
ID | Name | Age | Country |
---|---|---|---|
1 | Sofia | 12 | Germany |
2 | Sophina | 13 | UK |
3 | Sophie | 14 | USA |
SQL SELECT DISTINCT Statement:
The select distinct statement is used to return only distinct values
SELECT DISTINCT Syntax::
SELECT DISTINCT column1, column2, ...
FROM table_name;
Student_Name | Gender | Home_Town |
---|---|---|
Rahul | Male | Lucknow |
Raj | Male | Varanasi |
Ramu | Male | Lucknow |
Example:
SELECT DISTINCT home_townFROM students
Home_Town |
Lucknow |
Varanasi |
SQL COUNT() Function:
The COUNT() function returns the number of rows in query
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SQL AVG() Function:
The AVG() function returns the average value of a numeric column.
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SQL Select As:
SQL Aliases
SQL AS is used to rename a table or a column temporarily by giving another name known as Alias.
Alias Column Syntax:
SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax:
SELECT column_name(s)
FROM table_name AS alias_name;
Example:
SELECT student_id AS id, student_name AS name FROM student;