Table of contents
1.
Introduction
2.
What is DESC Command in SQL?
3.
Parameters of DESC Command in SQL
4.
Uses of DESC Command in SQL
5.
Implementation of DESC in SQL with Examples
6.
 
7.
Frequently Asked Questions
7.1.
Q. What is the syntax of using the DESC command in SQL?
7.2.
Q. Can you get the detail of the constraint in the table using the DESC command in SQL?
7.3.
Q. When can you use the DESC command in SQL?
8.
Conclusion
Last Updated: May 22, 2024
Medium

DESC Command in SQL

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The DESC in SQL is used in the "ORDER BY" clause to sort query results in descending order, arranging data from highest to lowest. and it can describe a table's structure, showing column names and data types.

SQL stands for Structured Query Langauge. It is a database language mainly designed to maintain data in RDBMS(Relational Database Management System). The data in RDBMS is stored in the form of tables. It is the standard language for accessing and manipulating databases. We have multiple commands in SQL to access the database; one such command is the DESC command. 

desc command in sql

In this blog, we will discuss the desc command in SQL. We will also look at the implementation and examples for better understanding.

What is DESC Command in SQL?

A desc command is also called a describe command in SQL. The command is case-insensitive and can be used in any of the below forms.

Syntax:

Describe Table_Name;


Or 

Desc Table_Name;


The above two commands perform the same function and will produce the same result.

The Table_Name here refers to the name of the table. The Describe command in SQL is used to see the table's structure. The desc command gives you complete information about the structure we have given to the table while creating it. 

The database command will help us view the table in the describe tab. The table will not be available in the console of the system software. 

The command shows the column name and associated data type details, i.e., intvarchartextfloat, CLOBXMLCURSOR, etc.,also about the table's column null and not null constraint.

Describe command gives information about the attribute, the value, and the default value associated with the table. 

Also read, Natural Join in SQL

Parameters of DESC Command in SQL

The syntax of the DESC command is given below:

{DESC / DESCRIBE} [TABLE] 
[format] table_identifier [partition_spec] [col_name]

Let us see what are the parameters used here.

  • format: Defines the extra details regarding the desired output.
  • table_identifier: Specifies the name of the database table whose structure we want to display.
  • partition_spec: An optional parameter that specifies a list of key and value pairs for partitions.
  • col_name: The name of the column that has to be specifically detailed is specified here.

Must read topic: gzip command in linux

Uses of DESC Command in SQL

Describe, or DESC command is the same command you use to get the table's structure.

Below is the use of the DESC command in SQL;

  • The DESC command gives detailed information about the table and its attributes and values. 
     
  • The command is handy for getting information about the table that is currently present in the database. 
     
  • The command gives a brief description of the table and its null values. It provides information about the null constraint in the table. 
     
  • The DESC command is used to see the table's structure after the create command. 
     
  • The information about the data types associated with the table column can also be seen when we run the command. 
     
  • If the TYPE keyword associated with the syntax is not mentioned, it, by default, takes the value as the column. 
     
  • The DESC command in SQL gives you information about the stage properties in the table column. 

Implementation of DESC in SQL with Examples

It is important to see the implementation for a better understanding. Below is the implementation of the DESC command in SQL.

To implement the DESC command, we use the below syntax:

Syntax:

Desc Table_name;


Or 

Describe Table_name;


Let us first create a table in our database.

We will create a book table with columns such as bookid, bookname, bookauthor, and bookprice.

The query to create the above table is given below:

SQL Query:

create table books( bookid int, bookname varchar(50), bookauthor varchar(60), bookprice int);

 

To see the structure of the database, we will use the describe command:

SQL Query:

describe books;


Output

TABLE BOOKS

Result Set 1

Column Null? Type
BOOKID - NUMBER
BOOKNAME - VARCHAR2(50)
BOOKAUTHOR - VARCHAR2(60)
BOOKPRICE - NUMBER

We have created an emp table with columns as empid, ename, job, manager, sal, and deptno. 

In the below table, we have empid as the primary key. The query to create the table is given below:

SQL Query:

create table emp(empid int PRIMARY KEY, ename varchar(40), job varchar(40), manager varchar(40), sal int,deptno int);

 

To see the structure of the database, we will use the describe command. The command will also show the primary key constraint in the structure. We will also see that because of the primary key constraint, no null values will be allowed in that column. 

SQL Query

desc emp;


Output

TABLE EMP

Result Set 2

Column Null? Type
EMPID NOT NULL NUMBER
ENAME - VARCHAR2(40)
JOB - VARCHAR2(40)
MANAGER - VARCHAR2(40)
SAL - NUMBER
DEPTNO - NUMBER

 

Frequently Asked Questions

Q. What is the syntax of using the DESC command in SQL?

The syntax of using the desc command in SQL is desc table_name or describe table_name;

Q. Can you get the detail of the constraint in the table using the DESC command in SQL?

Using the DESC command, you can get complete information about the table that includes the constraint, the attribute, the null value, the primary key, etc. 

Q. When can you use the DESC command in SQL?

When there is no table in the database, you can use the DESC command after creating your table. If your table already exists in the database, you can use the DESC command directly. 

Conclusion

In this blog, we have discussed the DESC command in SQL. We have thoroughly addressed the use and implementation of command with a few examples.

To learn more about SQL, please refer to our blogs

Function v/s Procedure in SQL

SQL joins interview questions

SQL Minifier

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our coursesrefer to the mock test and problems look at the interview experiences and interview bundle for placement preparations.

Here are, Top 100 SQL Problems to get practical knowledge and build a better understanding of SQL.

Happy Coding!!

Live masterclass