Wednesday, 15 March 2017

SQL-SELECT

SQL SELECT:

The most commonly used SQL command is SELECT statement. It is used to query the database and retrieve selected data that follow the conditions we want.
SELECT expressions  
FROM tables  
WHERE conditions;  

Optional clauses in SELECT statement:

There are some optional clauses in SELECT statement:
[WHERE Clause] : It specifies which rows to retrieve.
[GROUP BY Clause] : Groups rows that share a property so that the aggregate function can be applied to each group.
[HAVING Clause] : It selects among the groups defined by the GROUP BY clause.
[ORDER BY Clause] : It specifies an order in which to return the rows.

SQL SELECT UNIQUE:

Actually, there is no difference between DISTINCT and UNIQUE.
In simple words, we can say that SELECT UNIQUE statement is used to retrieve a unique or distinct element from the table.
SELECT UNIQUE column_name  
FROM table_name;  
SQL SELECT DISTINCT statement can also be used for the same cause.
SELECT DISTINCT column_name ,column_name  
FROM  table_name;  

SELECT DISTINCT home_town  
FROM students 

SQL SELECT TOP:

The SQL SELECT TOP Statement is used to select top data from a table. The top clause specifies that how many rows are returned.
SELECT TOP 2 * FROM employee ;

SQL SELECT IN:

SQL IN is an operator used in a SQL query to help reduce the need to use multiple SQL "OR" conditions.
It is used in SELECT, INSERT, UPDATE or DELETE statement.
Expression IN (value 1, value 2 ... value n);  
SELECT *  
FROM students  
WHERE students_name IN ( Amit , Raghav, Rajeev)  

SELECT *  
FROM marks  
WHERE roll_no IN (001, 023, 024);  

 

SQL SELECT NULL:

There can be two conditions:
  1. Where SQL is NULL
  2. Where SQL is NOT NULL
If in a table, a column is optional, it is very easy to insert data in column or update an existing record without adding a value in this column. This means that field has null value.

Where SQL is NULL:

How to select records with null values only? (in the marks column)
There is an example of student table:
SELECT EMP_ID,NAME, AGE, SALARY FROM EMPLOYEE  WHERE SALARY IS NULL  

Where SQL is NOT NULL:

How to select with no null values(in marks column)? Let's see the query to get all the records where marks is NOT NULL
SELECT EMP_ID,NAME, AGE, SALARY FROM EMPLOYEE    

WHERE SALARY IS NOT NULL   

No comments:

Post a Comment