Query/Access/Retrieve data from a table using SQL

SQL is a programming language used to access and manipulate data held in relational databases. It is a crucial tool for data analysis and reporting since it enables users to extract and filter data based on particular criteria. A statement used to obtain information from a database is known as a SQL query.

The syntax for a basic SQL query is as follows:

SELECT column1, column2, … FROM table_name WHERE condition;

The columns to obtain from the database are specified in the SELECT query. Which table to retrieve the data from is specified in the FROM statement. Based on a number of criteria, the WHERE statement determines which rows to obtain.

Suppose a table called customers with columns customer_id, first_name, last_name, email and phone. Retrieve all customers' data using the following query:

SELECT * FROM customers;

The * symbol in the SELECT statement is a wildcard character that specifies to retrieve all columns from the table.

Filtering Data

Use WHERE statement to filter data based on specific conditions. For example, to retrieve all customers who live in the India. Use the following query:

SELECT * FROM customers WHERE country = 'India';

The WHERE statement specifies that only retrieve rows where the country column is equal to 'India.'

Sorting Data

Use ORDER BY statement to sort data based on one or more columns. For example, retrieve all customer's data sorted by their last name in ascending order. Use following query:

SELECT *

FROM customers

ORDER BY last_name ASC;

The ASC keyword specifies that we want to sort the data in ascending order. Use the DESC keyword to sort data in descending order.

Limiting Data

Use LIMIT statement to limit the number of rows returned by a query. For example, retrieve first ten customer's data. Use following query:

SELECT * FROM customers LIMIT 10;

Comments

Popular posts from this blog

OWASP Top 10

TCP/IP Model

AAA