0% found this document useful (0 votes)
10 views16 pages

SQL Query Syntax and Functions Guide

The document discusses various SQL queries and functions that can be used to retrieve, manipulate, and summarize data stored in a database. It provides 23 examples of common SQL queries and functions such as SELECT, INSERT, UPDATE, DELETE, DISTINCT, WHERE, AND/OR, IN, BETWEEN, LIKE, COUNT, ORDER BY, GROUP BY, HAVING, JOIN, CREATE VIEW, and others. The purpose of the assignment is to understand and demonstrate proficiency with these SQL queries by creating a database and running the queries to obtain result screenshots.

Uploaded by

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

SQL Query Syntax and Functions Guide

The document discusses various SQL queries and functions that can be used to retrieve, manipulate, and summarize data stored in a database. It provides 23 examples of common SQL queries and functions such as SELECT, INSERT, UPDATE, DELETE, DISTINCT, WHERE, AND/OR, IN, BETWEEN, LIKE, COUNT, ORDER BY, GROUP BY, HAVING, JOIN, CREATE VIEW, and others. The purpose of the assignment is to understand and demonstrate proficiency with these SQL queries by creating a database and running the queries to obtain result screenshots.

Uploaded by

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

Data Warehousing

Assignment # 01
The purpose of this assignment is to understand following SQL queries syntax and the purpose.
You have to make a database of your choice in Access or SQL Server and run the following SQL
queries on it. The output of this assignment is a document containing the SQL queries plus the
screen shots of the output tables.

SQL Query has run in python interpreter using XAMPP server

1. Select
The SELECT statement is used to select data from a database.
2. Insert
The INSERT INTO statement is used to insert new records in a table.
3. Update
The UPDATE statement is used to modify the existing records in a table.

4. Delete
The DELETE statement is used to delete existing records in a table.
5. Distinct
The SELECT DISTINCT statement is used to return only distinct (different) values.
6. Where clause
The WHERE clause is used to filter records.
7. AND/OR
The AND and OR operators are used to filter records based on more than one condition:

 The AND operator displays a record if all the conditions separated by AND are TRUE.

 The OR operator displays a record if any of the conditions separated by OR is TRUE.

8. IN
The IN operator allows to specify multiple values in a WHERE clause.
9. ALIAS
SQL aliases are used to give a table, or a column in a table, a temporary name.

10. BETWEEN
The BETWEEN operator selects values within a given range. 
11. LIKE
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

 The percent sign (%) represents zero, one, or multiple characters

 The underscore sign (_) represents one, single character


12. COUNT
The COUNT() function returns the number of rows that matches a specified criterion

13. ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.

14. GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
15. HAVING
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate
functions.

16. INNER JOIN


The INNER JOIN keyword selects records that have matching values in both tables.

17. LEFT OUTER JOIN


The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from
the right table (table2). The result is 0 records from the right side, if there is no match.
18. RIGHT OUTER JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records
from the left table (table1). The result is 0 records from the left side, if there is no match.

19. FULL JOIN


The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
20. SELF JOIN
A self join is a regular join, but the table is joined with itself.

21. CREATE VIEW


In SQL, a view is a virtual table based on the result-set of an SQL statement.A view contains rows and
columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
22. SQL ARITHMETIC FUNCTIONS—AVG, COUNT etc
The COUNT() function returns the number of rows that matches a specified criterion.

The AVG() function returns the average value of a numeric column. 


23. SQL STRING FUNCTIONS
ASCII: Returns the ASCII value for the specific character

CHAR Length: Returns the length of a string (in characters)

CONCATICATE: Adds two or more expressions together

TRIM: Removes leading and trailing spaces from a string

UPPER: Converts a string to upper-case

LOWER: Converts a string to lower-case


24. SQL UNION
The UNION operator is used to combine the result-set of two or more SELECT statements.

Common questions

Powered by AI

An INNER JOIN is preferred when you want to return only the records with matching values in both tables, which is suitable for ensuring data integrity and consistency. On the other hand, an OUTER JOIN (LEFT, RIGHT, or FULL) is used when you need to retrieve all records from one table and the matching ones from the other table, which is vital for comprehensive reports that require all entries, regardless of matching data in the second table. The choice impacts the result set's size and completeness, with INNER JOIN providing potentially fewer but more accurate results and OUTER JOINS offering a broader view albeit with possible NULL entries .

SQL string functions such as CONCATE and TRIM greatly enhance data manipulation by addressing common text data issues. CONCATE solves the problem of needing a combined output from multiple string expressions, facilitating the merging of textual data from columns or expressions. TRIM is particularly useful for correcting space-related inconsistencies by removing leading and trailing spaces, which can occur due to manual data entry errors or formatting discrepancies, thereby ensuring cleaner, more uniform data outputs .

A SELF JOIN is used when comparing or relating rows within the same table, essentially treating the table as two separate entities within a single query. It leverages table aliases to differentiate the same table’s multiple appearances in the query, which is crucial for readability and avoiding confusion. Practical scenarios include hierarchical data analysis, such as organizational structures or tree-based data, permitting comparisons like managers to employees. Implementing SELF JOINS involves careful aliasing to maintain clear perspective regarding which instance of the table rows are being operated on .

SQL arithmetic functions such as AVG and COUNT provide distinct advantages by facilitating aggregate data analysis within large datasets. AVG calculates the average value of a specified numeric column, useful for financial analysis or performance metrics, while COUNT determines the number of rows meeting certain criteria, aiding in item inventory or number tally tasks. The two functions differ in application: AVG offers insights into trends within numeric data, whereas COUNT provides a straightforward tally without considering numeric properties .

The LIKE operator, enhanced by wildcards such as % and _, provides flexibility in pattern-based searches, useful for finding matches within partial strings (e.g., searching for names starting with 'Jo'). This contrasts with the = operator, which is limited to exact matches. LIKE is particularly valuable in text pattern recognition and filtering scenarios where precision is essential, such as searching through articles or identifying variations of a name. The = operator, however, is preferable when exact values are required, like exact product IDs or unambiguous fields .

The SQL UNION operator is used to combine the result sets of two or more SELECT statements. It requires that each SELECT statement within the UNION must have the same number of columns in the same order with similar data types. This operator eliminates duplicate records by default unless UNION ALL is used. Care must be taken to ensure that the data types are compatible across combined tables and that the intended logical flow is maintained across result sets to avoid unexpected outcomes or data loss .

The WHERE clause in SQL is crucial for filtering records by specifying conditions. It is essential when combined with AND/OR operators because it allows for precise data retrieval by filtering records based on multiple conditions. The AND operator ensures that a record is displayed only when all specified conditions are true, while the OR operator displays a record if any condition is true. This capability to combine multiple conditions enhances data retrieval efficiency and accuracy .

The GROUP BY statement in SQL is essential for aggregating data. It groups rows that have the same values in specified columns into summary rows, and is often used with aggregate functions such as COUNT, SUM, AVG, MAX, MIN. Conversely, ORDER BY is used to sort the result-set either in ascending or descending order. While GROUP BY clusters data for summary insights, ORDER BY is used to organize the data presentation. Their use cases differ in that GROUP BY is integral for analysis requiring grouped data assessments, while ORDER BY enhances data readability and ordered output .

A SQL VIEW offers the benefit of simplified query complexity by presenting a virtual table that consolidates data logically from one or more tables into accessible formats. It provides data abstraction and can hide the complexity of underlying tables. However, VIEWs generally cannot be indexed, which can limit performance; also, modifications on views with intricate joins or calculations can be restricted without losing data integrity. They are read-only unless updateable conditions are met, constraining their use in some transactional scenarios .

The HAVING clause complements SQL by allowing conditions on aggregated data, which the WHERE clause cannot handle. HAVING filters records after aggregation, vital for tasks requiring dynamic control over summaries (e.g., finding departments with an average salary above a certain threshold). Aggregate functions operate on a set of results, which are processed after WHERE conditions are applied. HAVING works on the aggregated results, not individual records, distinguishing its purpose and scope from WHERE. This distinction is critical in generating accurate insights from complex datasets .

You might also like