DESC
DESC is a SQL command used to describe the structure of a database table. It provides a listing of all columns, the datatype of each column, and any additional information related to those columns within a specified table.
Example
USE myDatabase;DESC myTable;
Output
+---------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+---------+-------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | varchar(45) | YES | | NULL | || age | int(11) | YES | | NULL | |+---------+-------------+------+-----+---------+----------------+
Explanation
The DESC
statement is used in MySQL to describe the structure of a specified table in the database. In this example, DESC myTable;
shows the columns (Field), data types (Type), whether NULL values are allowed (Null), the Key status of each column, the Default values, and any Extra information for each column in the myTable
within myDatabase
.
Example
DESC employees;
Output
name | type | nullable | default---------+---------+-----------+-----------id | integer | not null | nextval('employees_id_seq'::regclass)name | text | not null |address | text | not null |email | text | |
Explanation
DESC
is a command in PostgreSQL that provides the description of a table. It lists all columns, their data types, whether they can be null, and their default values, if any. In the above example, DESC employees;
is providing the description of the employees
table.
Example
SELECT * FROM CustomersORDER BY CustomerID DESC;
Output
+------------+-----------------+-------------+-----------+----------+| CustomerID | CustomerName | ContactName | Country | Phone |+------------+-----------------+-------------+-----------+----------+| 91 | Bon app | John Smith | USA | 12345678 || 90 | White Clover | Mary Johnson| Ireland | 23456789 || 89 | North/South | James Brown | UK | 34567891 |+------------+-----------------+-------------+-----------+----------+
Explanation
The command showcases the use of DESC keyword in SQL which sorts the results in a descending order as per the column mentioned after ORDER BY clause i.e., CustomerID in this scenario. After execution, the Customers table is displayed with records arranged in descending order of CustomerID.
Example
DESC employees;
Output
Name Null Type---------- -------- ------------EMPLOYEE_ID NOT NULL NUMBER(6)FIRST_NAME NULL VARCHAR2(20)LAST_NAME NOT NULL VARCHAR2(25)EMAIL NOT NULL VARCHAR2(25)PHONE_NUMBER NULL VARCHAR2(20)
Explanation
The DESC
command in SQL is used to display the structure of a table. It provides a detailed view of each attribute (column) in the table, including the type of data associated with the attribute and whether the attribute allows null values or not.
This example shows the structure of the employees
table.
Example
CREATE TABLE Employees ( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
INSERT INTO Employees (ID, NAME, AGE, ADDRESS, SALARY)VALUES (1, 'Paul', 32, 'California', 20000.00);
INSERT INTO Employees (ID, NAME, AGE, ADDRESS, SALARY)VALUES (2, 'Allen', 25, 'Texas', 15000.00);
SELECT * FROM EmployeesORDER BY SALARY DESC;
Output
ID NAME AGE ADDRESS SALARY---------- ---------- ---------- -------- --------1 Paul 32 California 20000.02 Allen 25 Texas 15000.0
Explanation
The SQL DESC command is used in the ORDER BY clause to sort the data in descending order. The example demonstrated sorts the column ‘SALARY’ in descending order. In the output, you can see that the employee with the highest salary is listed first.