t sql basic to advanced

 Transact-SQL (T-SQL) is the extension of SQL (Structured Query Language) used specifically for Microsoft SQL Server. T-SQL commands are used to interact with SQL Server databases and perform various operations. Here are some common T-SQL commands:


1. **SELECT**: Retrieve data from one or more database tables.

   ```

   SELECT column1, column2, ...

   FROM table_name;

   ```


2. **INSERT**: Insert new records into a table.

   ```

   INSERT INTO table_name (column1, column2, ...)

   VALUES (value1, value2, ...);

   ```


3. **UPDATE**: Modify existing records in a table.

   ```

   UPDATE table_name

   SET column1 = value1, column2 = value2, ...

   WHERE condition;

   ```


4. **DELETE**: Remove records from a table.

   ```

   DELETE FROM table_name

   WHERE condition;

   ```


5. **CREATE TABLE**: Create a new database table.

   ```

   CREATE TABLE table_name (

       column1 data_type constraint,

       column2 data_type constraint,

       ...

   );

   ```


6. **ALTER TABLE**: Modify an existing table structure.

   ```

   ALTER TABLE table_name

   ADD column_name data_type constraint;


   ALTER TABLE table_name

   ALTER COLUMN column_name data_type;


   ALTER TABLE table_name

   DROP COLUMN column_name;

   ```


7. **DROP TABLE**: Delete a table from the database.

   ```

   DROP TABLE table_name;

   ```


8. **CREATE DATABASE**: Create a new database.

   ```

   CREATE DATABASE database_name;

   ```


9. **USE**: Switch to a specific database.

   ```

   USE database_name;

   ```


10. **TRUNCATE TABLE**: Remove all rows from a table, but keep the table structure.

    ```

    TRUNCATE TABLE table_name;

    ```


11. **JOIN**: Combine rows from two or more tables based on a related column.

    ```

    SELECT column1, column2, ...

    FROM table1

    INNER JOIN table2 ON table1.column_name = table2.column_name;

    ```


12. **GROUP BY**: Group rows based on the values of a column.

    ```

    SELECT column1, COUNT(*)

    FROM table_name

    GROUP BY column1;

    ```


13. **HAVING**: Filter the results of a GROUP BY clause.

    ```

    SELECT column1, COUNT(*)

    FROM table_name

    GROUP BY column1

    HAVING COUNT(*) > 1;

    ```


14. **ORDER BY**: Sort the result set in ascending or descending order.

    ```

    SELECT column1, column2, ...

    FROM table_name

    ORDER BY column1 ASC; -- or DESC for descending order

    ```


15. **CREATE INDEX**: Create an index on one or more columns to improve query performance.

    ```

    CREATE INDEX index_name

    ON table_name (column1, column2, ...);

    ```


These are some of the fundamental T-SQL commands used to interact with Microsoft SQL Server databases. Keep in mind that there are many other T-SQL commands and functionalities available to manage and query databases efficiently.

Post a Comment

0 Comments