Skip to content
Related Articles

Related Articles

How to Select Individual Columns in SQL?

Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 09 Nov, 2021

In SQL, sometimes we require to select individual columns from a table. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Select is the most commonly used statement in SQL. The SELECT Statement in SQL is used to retrieve or fetch data from a database. We can fetch either the entire table or according to some specified rules. The data returned is stored in a result table. This result table is also called the result-set.

Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.

Query:

CREATE DATABASE GeeksForGeeks

Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.

Query:

USE GeeksForGeeks

Output:

Step 3: Create a table of RESIDENTS inside the database GeeksForGeeks. This table has 3 columns namely R_NAME, AGE, and FLAT containing the name, age, and a flat number of various residents of society.

Query:

CREATE TABLE RESIDENTS(
R_NAME VARCHAR(20),
AGE INT,
FLAT INT);

Output:

Step 4: Describe the structure of the table RESIDENTS.

Query:

EXEC SP_COLUMNS RESIDENTS;

Output:

Step 5: Insert 5 rows into the RESIDENTS table.

Query:

INSERT INTO RESIDENTS VALUES('MICHAEL',45,904);
INSERT INTO RESIDENTS VALUES('GARY',90,1105);
INSERT INTO RESIDENTS VALUES('SCOTT',39,806);
INSERT INTO RESIDENTS VALUES('JIM',34,301);
INSERT INTO RESIDENTS VALUES('PAMELA',29,502);

Output:

Step 6: Display all the rows of the RESIDENTS table.

Query:

SELECT * FROM RESIDENTS;

Output:

Step 7: For selecting an individual column from the table, we need to write the column’s name to be selected after the keyword SELECT followed by the rest of the query. For example in the below illustration, the column R_NAME is an individual column being selected from the table RESIDENTS.

Syntax:

SELECT COLUMN_NAME FROM TABLE_NAME;

Query:

SELECT R_NAME FROM RESIDENTS;

Output:

Step 8:

Select the individual column AGE from the table RESIDENTS.

Query:

SELECT AGE FROM RESIDENTS;

Output:

Step 9:

Select the individual column FLAT from the table RESIDENTS.

Query:

SELECT FLAT FROM RESIDENTS;

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!