Fundamentals of Transact-SQL (T-SQL) Queries

Duc Dang - Aug 18 - - Dev Community

Basic Transact-SQL (T-SQL) Queries

Transact-SQL (T-SQL) is an extension of SQL (Structured Query Language) used primarily with Microsoft SQL Server. It adds procedural programming, local variables, and various support functions to the standard SQL. Whether you’re a beginner or looking to brush up on your skills, understanding basic T-SQL queries is essential for managing and manipulating data effectively. In this post, we’ll cover some fundamental T-SQL queries that every database professional should know.

What is T-SQL?

T-SQL is Microsoft’s proprietary extension to SQL. It includes additional features that allow for more complex and powerful database operations. T-SQL is used to interact with SQL Server databases, enabling you to perform tasks such as querying data, updating records, and managing database objects.

Basic T-SQL Queries

1. SELECT Statement

The SELECTstatement is used to retrieve data from one or more tables. It is the most commonly used query in T-SQL.

SELECT column1, column2
FROM table_name;
Enter fullscreen mode Exit fullscreen mode

2. WHERE Clause

The WHERE clause is used to filter records based on specific conditions.

SELECT column1, column2
FROM table_name
WHERE condition;
Enter fullscreen mode Exit fullscreen mode

3. ORDER BY Clause

The ORDER BY clause is used to sort the result set in ascending or descending order.

SELECT column1, column2
FROM table_name
ORDER BY column1 ASC;
Enter fullscreen mode Exit fullscreen mode

4. INSERT INTO Statement

The INSERT INTO statement is used to add new records to a table.

INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Enter fullscreen mode Exit fullscreen mode

5. UPDATE Statement

The UPDATE statement is used to modify existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Enter fullscreen mode Exit fullscreen mode

6. DELETE Statement

The DELETE statement is used to remove records from a table.

DELETE FROM table_name
WHERE condition;
Enter fullscreen mode Exit fullscreen mode

7. JOIN Clause

The JOIN clause is used to combine rows from two or more tables based on a related column.

SELECT a.column1, b.column2
FROM table1 a
JOIN table2 b ON a.common_column = b.common_column;
Enter fullscreen mode Exit fullscreen mode

8. GROUP BY Clause

The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows.

SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
Enter fullscreen mode Exit fullscreen mode

9. HAVING Clause

The HAVING clause is used to filter groups based on a specified condition.

SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING COUNT(*) > 1;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering basic T-SQL queries is a fundamental step in becoming proficient with SQL Server. These queries form the foundation for more advanced database operations and are essential for data retrieval, manipulation, and management. Practice these queries regularly to enhance your skills and efficiency in working with SQL Server.

Stay tuned for more advanced T-SQL tutorials and tips to further your database expertise!

. . . . . . . . . . .