Note: Unlike some query languages, T-SQL does not require a semi-colon at the end of a statement. Therefore, the semi-colon shown in the example above is optional. You may also notice that SQL keywords are not case-sensitive. For example, "create table" would produce the same result as "CREATE TABLE," as shown above.
1.4 Insert Solution
--Using standard syntaxINSERT dbo.Products (ProductID, ProductName, Price, ProductDescription)VALUES (1, 'Clamp', 12.48, 'Workbench clamp');--Changing the order of the columnsINSERT dbo.Products (ProductName, ProductID, ProductDescription, Price)VALUES ('Clamp', 2, 'Workbench clamp', 12.48);--Skipping the column list, but keeping the values in order INSERT dbo.ProductsVALUES (50, 'Flat Head Screwdriver', 3.25, 'Flat head');--Adding multiple items in the same statementINSERT ProductsVALUES (51, 'Screwdriver', 3.25, 'Flat head'), (52, 'Screwdriver', 3.17, 'Flat head');--Dropping the ProductDescription columnINSERT Products (ProductID, ProductName, ProductDescription)VALUES (75, 'Tire Bar', 'Tool for changing tires');
Note: You may notice that the last two examples do not include "dbo." in front of "Products." Adding "dbo." in front of the table name is optional in some query languages, including T-SQL.
1.5 Update Solution
--Change the ProductName by targeting the ProductIDUPDATE Products SET ProductName ='The Flattest Flat Head'WHERE ProductID =50; --Change the price of a specific ProductIDUPDATE ProductsSET Price ='12.75'WHERE ProductID =1;--Change anything with a specific price to a new priceUPDATE ProductsSET Price ='3.50'WHERE Price ='3.25';
1.6 Read Solution
--Print all products using standard syntaxSELECT ProductID, ProductName, Price, ProductDescriptionFROM Products;--Print all products using *SELECT*FROM PRODUCTS--Print ProductName and Price columnsSELECT ProductName, PriceFROM PRODUCTS;--Print all Products with a ProductID less than 60SELECT*FROM PRODUCTSWHERE ProductID <60;--Print all Products with a ProductID between 50 and 55 using BETWEEN and ANDSELECT*FROM PRODUCTSWHERE ProductID BETWEEN50AND55;--Print all Products with a ProductDescription that starts with Tool using % and LIKESELECT*FROM PRODUCTSWHERE ProductDescription LIKE'Tool%';--Print all ProductNames and Prices including a 7% tax in CustomerPays columnSELECT ProductName, ((.07* Price) + Price) AS CustomerPaysFROM PRODUCTS;