SQL View in Microsoft Access allows you to write and edit SQL statements directly. Instead of creating a query only by selecting fields in Design View, you can use SQL commands to retrieve, filter, sort, group, and modify data.
SQL stands for Structured Query Language. It is used to communicate with relational databases.
SQL View is the area in Access Query Design where you can view, write, and edit the SQL statement for a query.
SELECT *
FROM Students;
This query retrieves all fields and records from the Students table.
Follow these steps to open SQL View in MS Access:
You can also switch an existing query between Design View and SQL View using the View menu.
The SELECT statement retrieves data from one or more tables.
SELECT *
FROM Students;
The asterisk (*) means all columns.
SELECT StudentName, Course
FROM Students;
This query displays only the StudentName and Course fields.
SELECT StudentName, Fees
FROM Students;
Only the selected fields appear in the query results.
The WHERE clause selects records that meet a specified condition.
SELECT *
FROM Students
WHERE Course = "ADCA";
This query displays students whose Course is ADCA.
SELECT StudentName, Fees
FROM Students
WHERE Fees > 5000;
Only students whose Fees value is greater than 5000 are returned.
SELECT *
FROM Students
WHERE StudentID = 101;
This query retrieves the record whose StudentID is 101.
The ORDER BY clause sorts query results in ascending or descending order.
SELECT *
FROM Students
ORDER BY StudentName ASC;
SELECT StudentName, Fees
FROM Students
ORDER BY Fees DESC;
SELECT StudentName, Course, Fees
FROM Students
ORDER BY Course ASC, StudentName ASC;
ASC means ascending order.DESC means descending order.AND and OR combine multiple conditions in a WHERE clause.
SELECT *
FROM Students
WHERE Course = "ADCA"
AND Fees > 5000;
Both conditions must be true for a record to be included.
SELECT *
FROM Students
WHERE Course = "ADCA"
OR Course = "Tally Prime";
A record is included if either condition is true.
Aggregate functions perform calculations on a set of values and return a summary.
SELECT Count(StudentID) AS TotalStudents
FROM Students;
SELECT Sum(Fees) AS TotalFees
FROM Students;
SELECT Avg(Fees) AS AverageFees
FROM Students;
SELECT Max(Fees) AS HighestFee,
Min(Fees) AS LowestFee
FROM Students;
These queries return summary values instead of displaying every individual record.
GROUP BY combines records that have the same value in a specified field. It is commonly used with aggregate functions.
SELECT Course, Sum(Fees) AS TotalFees
FROM Students
GROUP BY Course;
This query displays each course once and calculates the total fees for that course.
SELECT Course, Count(StudentID) AS TotalStudents
FROM Students
GROUP BY Course;
This query shows how many student records belong to each course.
An UPDATE statement changes existing records. Use a WHERE clause when you intend to change only selected records.
UPDATE Students
SET Fees = 12000
WHERE StudentID = 101;
This statement sets the Fees value to 12000 for the record with StudentID 101.
| SQL View | Design View |
|---|---|
| Write SQL statements directly. | Build queries using a visual grid. |
| Useful for learning SQL syntax. | Useful for beginners who prefer a visual interface. |
| Allows direct editing of query SQL. | Allows fields and criteria to be selected visually. |
| Requires correct SQL syntax. | Access builds much of the SQL automatically. |
Both views can be useful. You can begin in Design View and switch to SQL View to examine or edit the generated SQL statement.
| Error | Possible Solution |
|---|---|
| Misspelled table name | Check the exact table name. |
| Misspelled field name | Check the field name in the table. |
| Missing quotation marks | Use appropriate delimiters for text values. |
| Incorrect clause order | Use the correct order of SQL clauses. |
| Wrong data type | Check whether the field contains text, numbers, or dates. |
| Missing WHERE condition | Review the intended records before executing an action query. |
Always check table names, field names, and data types when Access displays an SQL error.
Create a Students table containing StudentID, StudentName, Course, Fees, and AdmissionDate fields.
1. Which statement retrieves data from a table?
2. Which clause filters records according to a condition?
3. Which SQL statement changes existing records?