Author: alien

  • Khóa học miễn phí T-SQL – Sub-Queries nhận dự án làm có lương

    T-SQL – Sub-Queries



    A sub-query or Inner query or Nested query is a query within another SQL Server query and embedded within the WHERE clause. A sub query is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

    Sub queries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

    There are a few rules that sub queries must follow −

    • You must enclose a subquery in parenthesis.

    • A subquery must include a SELECT clause and a FROM clause.

    • A subquery can include optional WHERE, GROUP BY, and HAVING clauses.

    • A subquery cannot include COMPUTE or FOR BROWSE clauses.

    • You can include an ORDER BY clause only when a TOP clause is included.

    • You can nest sub queries up to 32 levels.

    Subqueries with SELECT Statement

    Syntax

    Subqueries are most frequently used with the SELECT statement. Following is the basic syntax.

    SELECT column_name [, column_name ]
    FROM   table1 [, table2 ]
    WHERE  column_name OPERATOR
       (SELECT column_name [, column_name ]
       FROM table1 [, table2 ]
       [WHERE])
    

    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
    

    Let us apply the following subquery with SELECT statement.

    SELECT *
       FROM CUSTOMERS
       WHERE ID IN (SELECT ID FROM CUSTOMERS WHERE SALARY > 4500)
    

    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
    7   Muffy      24        Indore           10000.00
    

    Subqueries with INSERT Statement

    Sub queries also can be used with INSERT statements. The INSERT statement uses the data returned from the subquery to insert into another table. The selected data in the subquery can be modified with any of the character, date, or number functions.

    Syntax

    Following is the basic syntax.

    INSERT INTO table_name [ (column1 [, column2 ]) ]
       SELECT [ *|column1 [, column2 ]
       FROM table1 [, table2 ]
       [ WHERE VALUE OPERATOR ]
    

    Example

    Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table. Following is the syntax to copy complete CUSTOMERS table into CUSTOMERS_BKP.

    INSERT INTO CUSTOMERS_BKP
       SELECT * FROM CUSTOMERS
       WHERE ID IN (SELECT ID FROM CUSTOMERS)
    

    Subqueries with UPDATE Statement

    The subquery can be used in conjunction with the UPDATE statement. Either single or multiple columns in a table can be updated when using a subquery with the UPDATE statement.

    Syntax

    Following is the basic syntax.

    UPDATE table
    SET column_name = new_value
    [ WHERE OPERATOR [ VALUE ]
       (SELECT COLUMN_NAME
       FROM TABLE_NAME)
       [ WHERE) ]
    

    Example

    Let us assume we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table.

    Following command example updates SALARY by 0.25 times in CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.

    UPDATE CUSTOMERS
       SET SALARY = SALARY * 0.25
       WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >= 27 )
    

    This will impact two rows and finally CUSTOMERS table will have the following records.

    ID  NAME       AGE       ADDRESS             SALARY
    1   Ramesh     32        Ahmedabad           500.00
    2   Khilan     25        Delhi               1500.00
    3   kaushik    23        Kota                2000.00
    4   Chaitali   25        Mumbai              6500.00
    5   Hardik     27        Bhopal              2125.00
    6   Komal      22        MP                  4500.00
    7   Muffy      24        Indore              10000.00
    

    Subqueries with DELETE Statement

    The subquery can be used in conjunction with the DELETE statement like with any other statements mentioned above.

    Syntax

    Following is the basic syntax.

    DELETE FROM TABLE_NAME
    [ WHERE OPERATOR [ VALUE ]
       (SELECT COLUMN_NAME
       FROM TABLE_NAME)
       [ WHERE) ]
    

    Example

    Let us assume we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table.

    Following command example deletes records from CUSTOMERS table for all the customers whose AGE is greater than or equal to 27.

    DELETE FROM CUSTOMERS
       WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >=27 )
    

    This would impact two rows and finally CUSTOMERS table will have the following records.

    ID  NAME       AGE       ADDRESS          SALARY
    2   Khilan     25        Delhi            1500.00
    3   kaushik    23        Kota             2000.00
    4   Chaitali   25        Mumbai           6500.00
    6   Komal      22        MP               4500.00
    7   Muffy      24        Indore           10000.00
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – DISTINCT Clause nhận dự án làm có lương

    T-SQL – DISTINCT Clause



    The MS SQL Server DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records.

    There may be a situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records.

    Syntax

    Following is the basic syntax of DISTINCT keyword to eliminate duplicate records.

    SELECT DISTINCT column1, column2,.....columnN
    FROM table_name
    WHERE [condition]
    

    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
    

    Let us see how the following SELECT query returns duplicate salary records.

    SELECT SALARY FROM CUSTOMERS
       ORDER BY SALARY
    

    The above command will produce the following output where salary 2000 comes twice which is a duplicate record from the original table.

    SALARY
    1500.00
    2000.00
    2000.00
    4500.00
    6500.00
    8500.00
    10000.00
    

    Let us now use DISTINCT keyword with the above SELECT query and see the result.

    SELECT DISTINCT SALARY FROM CUSTOMERS
       ORDER BY SALARY
    

    The above command produces the following output where we do not have any duplicate entry.

    SALARY
    1500.00
    2000.00
    4500.00
    6500.00
    8500.00
    10000.00
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – ORDER BY Clause nhận dự án làm có lương

    T-SQL – ORDER BY Clause



    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
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – WHERE Clause nhận dự án làm có lương

    T-SQL – WHERE Clause



    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
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – GROUP BY Clause nhận dự án làm có lương

    T-SQL – GROUP BY Clause



    The SQL Server GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups.

    The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.

    Syntax

    Following is the basic syntax of GROUP BY clause. The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used.

    SELECT column1, column2
    FROM table_name
    WHERE [ conditions ]
    GROUP BY column1, column2
    ORDER BY column1, column2
    

    Example

    Consider the CUSTOMERS table is 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
    

    If you want to know the total amount of salary on each customer, then following will be the GROUP BY query.

    SELECT NAME, SUM(SALARY) as [sum of salary] FROM CUSTOMERS
       GROUP BY NAME;
    

    The above command will produce the following output.

    NAME        sum of salary
    Chaitali    6500.00
    Hardik      8500.00
    kaushik     2000.00
    Khilan      1500.00
    Komal       4500.00
    Muffy       10000.00
    Ramesh      2000.00
    

    Let us now consider the following CUSTOMERS table having the following records with duplicate names.

    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
    

    If we want to know the total amount of salary on each customer, then following will be GROUP BY query.

    SELECT NAME, SUM(SALARY) as [sum of salary] FROM CUSTOMERS
       GROUP BY NAME
    

    The above command will produce the following output.

    NAME        sum of salary
    Hardik      8500.00
    kaushik     8500.00
    Komal       4500.00
    Muffy       10000.00
    Ramesh      3500.00
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – LIKE Clause nhận dự án làm có lương

    T-SQL – LIKE Clause



    The MS SQL Server LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator −

    • The percent sign (%)
    • The underscore (_)

    The percent sign represents zero, one, or multiple characters. The underscore represents a single number or character. The symbols can be used in combinations.

    Syntax

    Following is the basic syntax of % and _.

    SELECT *column-list FROM table_name
    WHERE column LIKE ''XXXX%''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''%XXXX%''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''XXXX_''
    
    or
    
    SELECT *column-list FROM table_name
    WHERE column LIKE ''_XXXX''
    
    or
    
    SELECT  *column-list FROM table_name
    WHERE column LIKE ''_XXXX_''
    

    You can combine N number of conditions using AND or OR operators. XXXX could be any numeric or string value.

    Example

    Following are a number of examples showing WHERE part having different LIKE clause with ”%” and ”_” operators.

    Sr.No Statement & Description
    1

    WHERE SALARY LIKE ”200%”

    Finds any values that start with 200

    2

    WHERE SALARY LIKE ”%200%”

    Finds any values that have 200 in any position

    3

    WHERE SALARY LIKE ”_00%”

    Finds any values that have 00 in the second and third positions

    4

    WHERE SALARY LIKE ”2_%_%”

    Finds any values that start with 2 and are at least 3 characters in length

    5

    WHERE SALARY LIKE ”%2”

    Finds any values that end with 2

    6

    WHERE SALARY LIKE ”_2%3”

    Finds any values that have a 2 in the second position and end with a 3

    7

    WHERE SALARY LIKE ”2___3”

    Finds any values in a five-digit number that start with 2 and end with 3

    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 will display all the records from CUSTOMERS table where SALARY starts with 200.

    SELECT * FROM CUSTOMERS
    WHERE SALARY LIKE ''200%
    

    The above command will produce the following output.

    ID   NAME     AGE     ADDRESS       SALARY
    1    Ramesh   32      Ahmedabad     2000.00
    3    kaushik  23      Kota          2000.00
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – UPDATE Statement nhận dự án làm có lương

    T-SQL – UPDATE Statement



    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
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – INSERT Statement nhận dự án làm có lương

    T-SQL – INSERT Statement



    The SQL Server INSERT INTO statement is used to add new rows of data to a table in the database.

    Syntax

    Following are the two basic syntaxes of INSERT INTO statement.

    INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]
    VALUES (value1, value2, value3,...valueN);
    

    Where column1, column2,…columnN are the names of the columns in the table into which you want to insert data.

    You need not specify the column(s) name in the SQL query if you are adding values for all the columns of the table. But make sure the order of the values is in the same order as the columns in the table. Following is the SQL INSERT INTO syntax −

    INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
    

    Example

    Following statements will create six records in CUSTOMERS table −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (2, ''Khilan'', 25, ''Delhi'', 1500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (3, ''kaushik'', 23, ''Kota'', 2000.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (5, ''Hardik'', 27, ''Bhopal'', 8500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (6, ''Komal'', 22, ''MP'', 4500.00 );
    

    Syntax

    You can create a record in CUSTOMERS table using second syntax as follows −

    INSERT INTO CUSTOMERS VALUES (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    All the above statements will produce the following records in CUSTOMERS table −

    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
    

    Populate One Table Using Another Table

    You can populate data into a table through SELECT statement over another table provided another table has a set of fields, which are required to populate first table. Following is the syntax −

    INSERT INTO first_table_name
       SELECT column1, column2, ...columnN
          FROM second_table_name
          [WHERE condition];
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – Create Tables nhận dự án làm có lương

    T-SQL – Create Tables



    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.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí T-SQL – SELECT Statement nhận dự án làm có lương

    T-SQL – SELECT Statement



    SQL Server SELECT statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets.

    Syntax

    Following is the basic syntax of SELECT statement −

    SELECT column1, column2, columnN FROM table_name;
    

    Where, column1, column2…are the fields of a table whose values you want to fetch. If you want to fetch all the fields available in the field, then you can use the following syntax −

    SELECT * FROM table_name;
    

    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 of the customers available in CUSTOMERS table −

    SELECT ID, NAME, SALARY FROM CUSTOMERS;
    

    The above command will produce the following output.

    ID  NAME          SALARY
    1   Ramesh        2000.00
    2   Khilan        1500.00
    3   kaushik       2000.00
    4   Chaitali      6500.00
    5   Hardik        8500.00
    6   Komal         4500.00
    7   Muffy         10000.00
    

    If you want to fetch all the fields of CUSTOMERS table, then use the following query −

    SELECT * FROM CUSTOMERS;
    

    The above will produce the following output.

    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
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc