T-SQL – Discussion

Discuss T-SQL ”; Previous Next T-SQL (Transact-SQL) is an extension of SQL language. This tutorial covers the fundamental concepts of T-SQL such as its various functions, procedures, indexes, and transactions related to the topic. Each topic is explained using examples for easy understanding. Print Page Previous Next Advertisements ”;

T-SQL – ORDER BY Clause

T-SQL – ORDER BY Clause ”; Previous Next The MS SQL Server ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some database sort query results in ascending order by default. Syntax Following is the basic syntax of ORDER BY clause. SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort, that column should be in column-list. Example Consider the CUSTOMERS table having the following records − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00 Following command is an example, which would sort the result in ascending order by NAME and SALARY. SELECT * FROM CUSTOMERS ORDER BY NAME, SALARY The above command will produce the following output. ID NAME AGE ADDRESS SALARY 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 3 kaushik 23 Kota 2000.00 2 Khilan 25 Delhi 1500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00 1 Ramesh 32 Ahmedabad 2000.00 Following command is an example, which would sort the result in descending order by NAME. SELECT * FROM CUSTOMERS ORDER BY NAME DESC The above command will produce the following result − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 7 Muffy 24 Indore 10000.00 6 Komal 22 MP 4500.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 5 Hardik 27 Bhopal 8500.00 4 Chaitali 25 Mumbai 6500.00 Print Page Previous Next Advertisements ”;

T-SQL – Create Tables

T-SQL – Create Tables ”; Previous Next Creating a basic table involves naming the table and defining its columns and each column”s data type. The SQL Server CREATE TABLE statement is used to create a new table. Syntax Following is the basic syntax of CREATE TABLE statement − CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ….. columnN datatype, PRIMARY KEY( one or more columns )); CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer to understand with the following example. A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. You can check complete details at Create Table Using another Table. Example In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table − CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID)); You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the following command − exec sp_columns CUSTOMERS The above command produces the following output. TABLE_QUALIFIER TABLE_OWNER TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME PRECISION LENGTH SCALE RADIX NULLABLE REMARKS COLUMN_DEF SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION IS_NULLABLE SS_DATA_TYPE TestDB dbo CUSTOMERS ID 4 int 10 4 0 10 0 NULL NULL 4 NULL NULL 1 NO 56 TestDB dbo CUSTOMERS NAME 12 varchar 20 20 NULL NULL 0 NULL NULL 12 NULL 20 2 NO 39 TestDB dbo CUSTOMERS AGE 4 int 10 4 0 10 0 NULL NULL 4 NULL NULL 3 NO 56 TestDB dbo CUSTOMERS ADDRESS 1 char 25 25 NULL NULL 1 NULL NULL 1 NULL 25 4 YES 39 TestDB dbo CUSTOMERS SALARY 3 decimal 18 20 2 10 1 NULL NULL 3 NULL NULL 5 YES 106 You can now see that CUSTOMERS table is available in your database which you can use to store required information related to customers. Print Page Previous Next Advertisements ”;

T-SQL – WHERE Clause

T-SQL – WHERE Clause ”; Previous Next The MS SQL Server WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables. If the given condition is satisfied, only then it returns a specific value from the table. You will have to use WHERE clause to filter the records and fetch only necessary records. The WHERE clause is not only used in SELECT statement, but it is also used in UPDATE, DELETE statement, etc., which we would examine in subsequent chapters. Syntax Following is the basic syntax of SELECT statement with WHERE clause − SELECT column1, column2, columnN FROM table_name WHERE [condition] You can specify a condition using comparison or logical operators like >, <, =, LIKE, NOT, etc. The following example will make this concept clear. Example Consider the CUSTOMERS table having the following records − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00 Following command is an example which would fetch ID, Name and Salary fields from the CUSTOMERS table where salary is greater than 2000. SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000; The above command will produce the following output. ID NAME SALARY 4 Chaitali 6500.00 5 Hardik 8500.00 6 Komal 4500.00 7 Muffy 10000.00 Following command is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table for a customer with the name ‘Hardik’. It is important to note that all the strings should be given inside single quotes (””) whereas numeric values should be given without any quote as in the above example − SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE NAME = ”Hardik”; The above command will produce the following output. ID NAME SALARY 5 Hardik 8500.00 Print Page Previous Next Advertisements ”;

T-SQL – UPDATE Statement

T-SQL – UPDATE Statement ”; Previous Next The SQL Server UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected. Syntax Following is the basic syntax of UPDATE query with WHERE clause − UPDATE table_name SET column1 = value1, column2 = value2…., columnN = valueN WHERE [condition]; You can combine N number of conditions using AND or OR operators. Example Consider the CUSTOMERS table having the following records − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00 Following command is an example, which would update ADDRESS for a customer whose ID is 6 − UPDATE CUSTOMERS SET ADDRESS = ”Pune” WHERE ID = 6; CUSTOMERS table will now have the following records − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Pune 4500.00 7 Muffy 24 Indore 10000.00 If you want to modify all ADDRESS and SALARY column values in CUSTOMERS table, you do not need to use WHERE clause. UPDATE query would be as follows − UPDATE CUSTOMERS SET ADDRESS = ”Pune”, SALARY = 1000.00; CUSTOMERS table will now have the following records. ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Pune 1000.00 2 Khilan 25 Pune 1000.00 3 kaushik 23 Pune 1000.00 4 Chaitali 25 Pune 1000.00 5 Hardik 27 Pune 1000.00 6 Komal 22 Pune 1000.00 7 Muffy 24 Pune 1000.00 Print Page Previous Next Advertisements ”;

T-SQL – DELETE Statement

T-SQL – DELETE Statement ”; Previous Next The SQL Server DELETE Query is used to delete the existing records from a table. You have to use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted. Syntax Following is the basic syntax of DELETE query with WHERE clause − DELETE FROM table_name WHERE [condition]; You can combine N number of conditions using AND or OR operators. Example Consider the CUSTOMERS table having the following records − ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 MP 4500.00 7 Muffy 24 Indore 10000.00 Following command is an example, which would DELETE a customer, whose ID is 6 − DELETE FROM CUSTOMERS WHERE ID = 6; CUSTOMERS table will now have the following records. ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 7 Muffy 24 Indore 10000.00 If you want to DELETE all the records from CUSTOMERS table, you do not need to use WHERE clause. DELETE query would be as follows − DELETE FROM CUSTOMERS; CUSTOMERS table now will not have any record. Print Page Previous Next Advertisements ”;