SQL Select Command
SQL Select Command is part of the SQL DDL commands. It is use to retrieve and fetch the required data from the database using the logical queries. It is the most important topic because our every query mostly going to have the Select Command.
Employees DataBase
To learn the further topics we are going to take one real time dataset which has more than millions of records. It is a complicated dataset to undserstand at first sight but once we go through it and use it in learning the fundamental concepts of sql, we will get use to it.
SELECT FROM
select .... from ..... helps us to fetch data from certain columns of a table.
Syntax
SELECT column_name_1, column_name_2 FROM table_name;
SELECT FROM WHERE
select .... from .... where helps us to get certain part of a column instead of complete column.
Syntax
SELECT column_name_1, column_name_2
FROM table_name
where condition;
AND
When both the condition is satisfied at that time. we will get the outptut.
Syntax
SELECT column_name_1, column_name_2
FROM table_name
where condition_1 and condition_2;
OR
When one of the given condition is satisfied we will get the outptut.
Syntax
SELECT column_name_1, column_name_2
FROM table_name
where condition_1 OR condition_2;
Order Precedence
AND is consider before OR, if both the operators are present in where clause.
Syntax
SELECT column_name_1, column_name_2
FROM table_name
where condition_1 OR condition_2 AND condition_3;
BETWEEN ... AND
It is help us to get the interval value between the two given values.
Both the values are also included.
Syntax
SELECT *
FROM table_name
where column_name BETWEEN value_1 AND value_2;
NOT BETWEEN ... AND
It is use to get the result before the first value and after the second value.
Both the values are not included.
Syntax
SELECT *
FROM table_name
where column_name NOT BETWEEN value_1 AND value_2;
IS NULL
It is use to fetch the records where certain column is null.
Syntax
SELECT *
FROM table_name
where column_name IS NULL;
IS NOT NUL
It is use to fetch the reocrds where certain columns are not null.
Syntax
SELECT *
FROM table_name
where column_name IS NOT NULL;
LIKE
It is use too fetch the records where certain column is like something using the wild characters like % and _
Syntax
SELECT *
FROM table_name
where column_name LIKE '_value';
NOT LIKE
It is use too fetch the records where certain column is not like something using the wild characters like % and _
Syntax
SELECT *
FROM table_name
where column_name NOT LIKE '%value';
Comparison Operators
=, !=, <>, >, <, >=, <=
Syntax
SELECT *
FROM table_name
where column_name > 'value';
We can use all the comparison operator like we had use the equal operator in the above example. Practice all the above comparison operators.
SELECT DISTINCT
It is use to select the distinct unique values present in a column.
Syntax
SELECT DISTINCT
column_name
FROM
table_name;
ORDER BY
It is use to order the fetched record. by default it will be ordered in ascending order and if you want to order in descending order by have to pass desc.
Syntax
SELECT
column_name
FROM
table_name
order by column name;
ALIASES
It is use to change the column name. using the keyword 'AS'
Syntax
SELECT
column_name AS new_name
FROM
table_name;
LIMIT
It is use to set the limit on number of records it is going to fetch.