0% found this document useful (0 votes)
531 views5 pages

SQL Query Basics and Examples

Uploaded by

kafeelhassan133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
531 views5 pages

SQL Query Basics and Examples

Uploaded by

kafeelhassan133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

SELECT Statement

Used to retrieve data from a table.

Syntax:

sql
Copy code
SELECT column1, column2 FROM table_name;

Example:

sql
Copy code
SELECT Name, Marks FROM Student;

✅ 2. SELECT * (All Columns)

Retrieves all columns from the table.

Syntax:

sql
Copy code
SELECT * FROM table_name;

Example:

sql
Copy code
SELECT * FROM Student;

✅ 3. WHERE Clause

Filters rows based on a condition.

Syntax:
sql
Copy code
SELECT column1 FROM table_name WHERE condition;

Example:

sql
Copy code
SELECT Name FROM Student WHERE Marks > 80;

✅ 4. DISTINCT Keyword

Removes duplicate values.

Syntax:

sql
Copy code
SELECT DISTINCT column_name FROM table_name;

Example:

sql
Copy code
SELECT DISTINCT Grade FROM Student;

✅ 5. ORDER BY Clause

Sorts the result in ascending (ASC) or descending (DESC) order.

Syntax:

sql
Copy code
SELECT column1 FROM table_name ORDER BY column1 ASC|DESC;

Example:
sql
Copy code
SELECT Name, Marks FROM Student ORDER BY Marks DESC;

✅ 6. IN Operator

Checks if a value matches any value in a list.

Syntax:

sql
Copy code
SELECT column1 FROM table_name WHERE column_name IN (value1, value2);

Example:

sql
Copy code
SELECT Name FROM Student WHERE Grade IN ('A', 'B');

✅ 7. BETWEEN Operator

Checks if a value is within a range.

Syntax:

sql
Copy code
SELECT column1 FROM table_name WHERE column_name BETWEEN value1 AND value2;

Example:

sql
Copy code
SELECT Name FROM Student WHERE Marks BETWEEN 70 AND 90;
✅ 8. LIKE Operator

Used for pattern matching with % and _.

Syntax:

sql
Copy code
SELECT column1 FROM table_name WHERE column_name LIKE 'pattern';

Example:

sql
Copy code
SELECT Name FROM Student WHERE Name LIKE 'A%'; -- Names starting with A

✅ 9. IS NULL Operator

Checks for NULL values.

Syntax:

sql
Copy code
SELECT column1 FROM table_name WHERE column_name IS NULL;

Example:

sql
Copy code
SELECT Name FROM Student WHERE Grade IS NULL;
✅ 10. Logical Operators (AND, OR, NOT)

Combine multiple conditions.

Syntax:

sql
Copy code
SELECT column1 FROM table_name WHERE condition1 AND/OR/NOT condition2;

Example:

sql
Copy code
SELECT Name FROM Student WHERE Marks > 80 AND Grade = 'A';

Common questions

Powered by AI

The ORDER BY clause does not change the results themselves but affects the order in which they are presented. Sorting is accomplished by specifying a column and a direction, either ascending (ASC) or descending (DESC). This is particularly useful for organizing data in a meaningful way for analysis or reporting .

The BETWEEN operator simplifies the syntax for checking if a value falls within a specified range, enhancing readability and efficiency of such queries. However, it might be used incorrectly if users assume it provides exclusive rather than inclusive bounds, potentially leading to incorrect data retrieval .

The IS NULL operator is critical in scenarios where the presence or absence of data needs to be evaluated, such as checking for incomplete data entries in a student's grade column. This is vital in ensuring data integrity, particularly in databases where null represents missing information .

The LIKE operator is used for pattern matching, ideal for searching text-based patterns using wildcards like '%' and '_'. It is limited in performance, especially with large datasets, because pattern matching can be resource-intensive. It is best used when searching for patterns in strings where exact matches are not known .

The IN operator simplifies the syntax when matching a column against multiple values, providing cleaner and potentially more efficient queries than using multiple OR conditions. It is preferable when checking for membership within a known list of values. However, if conditions are more complex or involve multiple columns, using OR might be necessary .

Multiple SELECT clauses can be used in subqueries or with set operations like UNION to combine results from different queries. They allow for complex data retrieval strategies where data needs to be aggregated, filtered, or combined from multiple queries, providing a comprehensive dataset that meets detailed analytical needs .

The DISTINCT keyword is useful in scenarios where duplicate values are not meaningful or required, such as when retrieving unique entries of a column like student grades. It ensures that only unique rows are returned. However, it can impact performance in large datasets due to the additional processing to identify and remove duplicates .

Logical operators such as AND, OR, and NOT allow for combining multiple conditions within a query, thereby enabling more complex and refined data filtering. This flexibility allows users to perform more specific data retrieval operations beyond what is possible with single conditions .

The SELECT statement is used to retrieve data from a table by specifying the columns of interest, whereas the WHERE clause is used to filter the rows returned by the SELECT statement based on specified conditions .

Using the SELECT * statement can be advantageous for its simplicity and ability to retrieve all columns without having to specify each one. However, it can be inefficient, especially in large tables, because it may retrieve more data than necessary, negatively affecting performance and consuming more resources .

You might also like