Basic Commands
Basic Commands
md 2024-09-04
Introduction to SELECT
The SELECT statement is fundamental in SQL, used to query data from one or more tables. It allows you to
specify which columns you want to retrieve and can be used to filter and sort the results.
To retrieve all columns from a table, use SELECT *. To select specific columns, list them separated by commas.
Note: To perform all the queries, we need a database and table where the data is
stored. This setup will enable us to practice all the SQL commands effectively.
Below, I will use SQL code to create a database, a table, and populate it with
sample data.
This will provide a practical environment for executing various SQL commands,
allowing you to test and understand how each command works.
First, create a new database and then a table within it. You can copy and paste this code to set up the
environment:
JoinDate DATE
);
The outputs shoud look like this after executing the commands :
When writing SQL commands, especially for creating tables, it's often helpful to format your code with
line breaks and indentation. This makes the code more readable and easier to debug.
2 / 15
Basic-commands.md 2024-09-04
In this formatted version, each column definition is on a new line. This clarity helps in several ways:
Readability: It's easier to see each column and its type, making the structure of the table clear at a glance.
Debugging: If there's an error or typo, it's simpler to identify and fix it. Each part of the table definition is
separate, so you can quickly locate and address issues.
Maintenance: When you need to modify the table structure later, having a well-organized format makes it
easier to make changes and understand the existing structure.
Although you can write the entire command on a single line, it can become challenging to spot errors or
understand the command, especially in more complex statements. Therefore, we recommend using a
formatted approach for better clarity and ease of use.
3 / 15
Basic-commands.md 2024-09-04
SELECT DISTINCT
The SELECT DISTINCT statement ensures that only unique values are returned, eliminating duplicate rows
from the results.
Example:
Result:
4 / 15
Basic-commands.md 2024-09-04
WHERE Clause
The WHERE clause filters records that meet specific criteria. It's used to specify the conditions for selecting
rows.
Example:
Result:
5 / 15
Basic-commands.md 2024-09-04
= : Equal
> : Greater than
< : Less than
>= : Greater than or equal
<= : Less than or equal
<> or != : Not equal (We can choose any of them)
BETWEEN : Between a certain range
LIKE : Search for a pattern
IN : Specify multiple possible values
Example:
Result:
-- To find the Names end with 'J' we will write `%j` and for finding any
specific word or charecter in btw names we will write '%j%' .
6 / 15
Basic-commands.md 2024-09-04
Result:
Result:
7 / 15
Basic-commands.md 2024-09-04
ORDER BY
The ORDER BY clause sorts the result set based on one or more columns. You can sort in ascending (ASC) or
descending (DESC) order.
Example:
Result:
8 / 15
Basic-commands.md 2024-09-04
These logical operators are used in the WHERE clause to combine or negate conditions.
Example:
To find employees in the 'IT' department with a salary greater than 60000:
Result:
9 / 15
Basic-commands.md 2024-09-04
Result:
INSERT INTO
The INSERT INTO statement adds new rows to a table. You need to specify the columns and values for the
new row.
Example:
10 / 15
Basic-commands.md 2024-09-04
Updated Table:
Before
After
NULL Values
11 / 15
Basic-commands.md 2024-09-04
NULL indicates the absence of a value. It's not the same as an empty string or zero. Use IS NULL or IS NOT
NULL to check for NULL values.
Example:
Result:
UPDATE Statement
The UPDATE statement is used to change existing records. You must specify which rows to update and what
new values to set.
Example:
UPDATE Employees
SET Salary = 50000
WHERE FirstName = 'James' AND LastName = 'Brown';
Updated Table:
12 / 15
Basic-commands.md 2024-09-04
DELETE Statement
The DELETE statement is used to remove rows from a table. Be careful with DELETE as it permanently removes
data.
Example:
Updated Table:
13 / 15
Basic-commands.md 2024-09-04
SELECT TOP
The SELECT TOP clause limits the number of rows returned in the result set. This is particularly useful for
pagination or when you need a subset of data.
Example:
Result:
14 / 15
Basic-commands.md 2024-09-04
15 / 15