Monday, February 9, 2015

SQL SELECT COUNT


SQL SELECT COUNT


The SQL COUNT() function is used to return the number of rows in a query. The count() function is generally used in SQL SELECT function. It is very useful to count the numbers of records.
Let's see the syntax of SQL COUNT statement.
  1. SELECT COUNT (expression)  
  2. FROM tables  
  3. WHERE conditions;  
Let's see the examples of sql select count function.

SQL SELECT COUNT(column_name)

  1. SELECT COUNT(nameFROM employee_table;  
It will return the total number of names of employee_table. But null fields will not be counted.

SQL SELECT COUNT(*)

  1. SELECT COUNT(*) FROM employee_table;  
The "select count(*) from table" is used to return the number of records in table.

SQL SELECT COUNT(DISTINCT column_name)

  1. SELECT COUNT(DISTINCT nameFROM employee_table;  
It will return the total distinct names of employee_table.

SQL SELECT DISTINCT


SQL SELECT DISTINCT


The SQL DISTINCT command is used with SELECT key word to retrieve only distinct or unique data.
In a table, there may be a chance to exist a duplicate value and sometimes we want to retrieve only unique values. In such scenarios, SQL SELECT DISTINCT statement is used.

Note: SQL SELECT UNIQUE and SQL SELECT DISTINCT statements are same.

Let's see the syntax of select distinct statement.
  1. SELECT DISTINCT column_name ,column_name  
  2. FROM  table_name;  
Let's try to understand it by the table given below:
Student_NameGenderMobile_NumberHOME_TOWN
Rahul OjhaMale7503896532Lucknow
Disha RaiFemale9270568893Varanasi
Sonoo JaiswalMale9990449935Lucknow
Here is a table of students from where we want to retrieve distinct information For example: distinct home-town.
  1. SELECT DISTINCT home_town  
  2. FROM students  
Now, it will return two rows.
HOME_TOWN
Lucknow
Varanasi

SQL SELECT UNIQUE


SQL SELECT UNIQUE


Actually, there is no difference between DISTINCT and UNIQUE.
SELECT UNIQUE is an old syntax which was used in oracle description but later ANSI standard defines DISTINCT as the official keyword.
After that oracle also added DISTINCT but did not withdraw the service of UNIQUE keyword for the sake of backward compatibility.
In simple words, we can say that the select statement used to query or retrieve data from a table in the database.
Let's see the syntax of select unique statement.
  1. SELECT UNIQUE column_name  
  2. FROM table_name;  

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.
In simple words, we can say that the select statement used to query or retrieve data from a table in the database.
Let's see the syntax of select statement.
  1. SELECT expressions  
  2. FROM tables  
  3. WHERE conditions;  
Here expression is the column that we want to retrieve.
Tables indicate the tables, we want to retrieve records from.

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.
For example, let a database table: student_details;
IDFirst_nameLast_nameAgeSubjectHobby
1AmarSharma20MathsCricket
2AkbarKhan22BiologyFootball
3AnthonyMilton25CommerceGambling
From the above example, select the first name of all the students. To do so, query should be like this:
  1. SELECT first_name FROM student_details;  
Note: the SQL commands are not case sensitive. We can also write the above SELECT statement as:
  1. select first_name from student_details;  
Now, you will get following data:
Amar
Akbar
Anthony
We can also retrieve data from more than one column. For example, to select first name and last name of all the students, you need to write
  1. SELECT first_name, last_name FROM student_details;  
Now, you will get following data:
AmarSharma
AkbarKhan
AnthonyMilton
We can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement.
Here a point is notable that only SELECT and FROM statements are necessary in SQL SELECT statements. Other clauses like WHERE, GROUP BY, ORDER BY, HAVING may be optional.

Saturday, February 7, 2015

SQL ALTER TABLE


SQL ALTER TABLE


The ALTER TABLE statement is used to add, modify or delete columns in an existing table. It is also used to rename a table.
You can also use SQL ALTER TABLE command to add and drop various constraints on an existing table.

SQL ALTER TABLE Add Column

If you want to add columns in SQL table, the SQL alter table syntax is given below:
  1. ALTER TABLE table_name ADD column_name column-definition;  
If you want to add multiple columns in table, the SQL table will be
  1. ALTER TABLE table_name   
  2. ADD (column_1 column-definition,  
  3.            column_2 column-definition,  
  4.            .....  
  5.            column_n column-definition);  

SQL ALTER TABLE Modify Column

If you want to modify an existing column in SQL table, syntax is given below:
  1. ALTER TABLE table_name MODIFY column_name column_type;  
If you want to modify multiple columns in table, the SQL table will be
  1. ALTER TABLE table_name   
  2. MODIFY (column_1 column_type,  
  3.                   column_2 column_type,  
  4.                  .....  
  5.                   column_n column_type);  

SQL ALTER TABLE DROP Column

The syntax of alter table drop column is given below:
  1. ALTER TABLE table_name DROP COLUMN column_name;  

SQL ALTER TABLE RENAME Column

The syntax of alter table rename column is given below:
  1. ALTER TABLE table_name  
  2. RENAME COLUMN old_name to new_name;  

SQL TEMP TABLE


SQL TEMP TABLE


The concept of temporary table is introduced by SQL server. It helps developers in many ways:
Temporary tables can be created at run-time and can do all kinds of operations that a normal table can do. These temporary tables are created inside tempdb database.
There are two types of temp tables based on the behavior and scope.
  1. Local Temp Variable
  2. Global Temp Variable

Local Temp Variable

Local temp tables are only available at current connection time. It is automatically deleted when user disconnects from instances. It is started with hash (#) sign.
  1. CREATE TABLE #local temp table (  
  2. User id int,  
  3. Username varchar (50),  
  4. User address varchar (150)  
  5. )  

Global Temp Variable

Global temp tables name starts with double hash (##). Once this table is created, it is like a permanent table. It is always ready for all users and not deleted until the total connection is withdrawn.
  1. CREATE TABLE ##new global temp table (  
  2. User id int,  
  3. User name varchar (50),  
  4. User address varchar (150)  
  5. )  

SQL COPY TABLE


SQL COPY TABLE


If you want to copy a SQL table into another table in the same SQL server database, it is possible by using the select statement.
The syntax of copying table from one to another is given below:
  1. Select * into <destination tablefrom <source table>  
For example, you can write following command to copy the records of hr_employee table into employee table.
  1. Select * into admin_employee from hr_employee;  

Note: SELECT INTO is totally different from INSERT INTO statement.

SQL TRUNCATE TABLE


SQL TRUNCATE TABLE


A truncate SQL statement is used to remove all rows (complete data) from a table. It is similar to the DELETE statement with no WHERE clause.

TRUNCATE TABLE Vs DELETE TABLE

Truncate table is faster and uses lesser resources than DELETE TABLE command.

TRUNCATE TABLE Vs DROP TABLE

Drop table command can also be used to delete complete table but it deletes table structure too. TRUNCATE TABLE doesn't delete the structure of the table.

Let's see the syntax to truncate the table from the database.
  1. TRUNCATE TABLE table_name;  
For example, you can write following command to truncate the data of employee table
  1. TRUNCATE TABLE Employee;  
Note: The rollback process is not possible after truncate table statement. Once you truncate a table you cannot use a flashback table statement to retrieve the content of the table.