Retrieving Data from Tables
Explore how to retrieve data from database tables using T-SQL SELECT statements. Learn techniques to select all columns or specific ones, use aliases to rename columns in output, and perform column operations like concatenation and data type conversion, equipping you to query tables effectively.
We'll cover the following...
We'll cover the following...
There is no use in having data if we cannot view it and work with it. We can use the SELECT command to retrieve data from tables in a database. Just like with inserting, there are several ways we can use this command.
Retrieve data from all columns
To retrieve all columns, we use this syntax:
SELECT * FROM TableSchema.TableName;
Let’s create the dbo.Students table, insert some data, and retrieve it afterward:
CREATE DATABASE LearningManagementSystem;
USE LearningManagementSystem;
CREATE TABLE Students
(
Id INT PRIMARY KEY IDENTITY(1, 1),
Name NVARCHAR(100) NOT NULL,
BirthDate DATETIME NOT NULL,
Address NVARCHAR(100) NOT NULL,
PhoneNumber VARCHAR(15) NULL
);
INSERT INTO dbo.Students (Address, Name, BirthDate)
VALUES ('Bishkek, Kyrgyzstan', 'Aidil Umarov', '1996-09-06');
-- Selecting all columns of the table
SELECT * FROM dbo.Students;Retrieving data from all columns
Retrieving all columns with * is not the best method of doing this, as we don’t usually need all ...