473,513 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic SQL Syntax for Access Queries

MMcCarthy
14,534 Recognized Expert Moderator MVP
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL:

Select Statement

SELECT [column_name] FROM [table_name];

Append Statement
INSERT INTO [table_name] ([column1], [column2], [column3])
VALUES ('value1', #value2#, value3);

This assumes value1 is a string, value2 is a date and value 3 is some other datatype


Update Statement

UPDATE [table_name] SET [column_name] = 'value1'
WHERE [other_column]=value2;

Delete Statement
DELETE * FROM [table_name];

Create Table Statement
SELECT Column1, Column2 INTO NewTable
FROM OldTable;

Distinct values only
SELECT DISTINCT [column_name] FROM [table_name];

Top 10 in an Ordered Query
SELECT TOP 10 [column_1], [column_2]
FROM [table_name]
ORDER BY [column_1];

Order by is Ascending by default. Use DESC at the of the statement to reverse the order.

IN Value List
SELECT [column_name]
FROM [table_name]
WHERE [column_name] IN ('value1', 'value2', 'value3');

Between Numbers
SELECT [column_name]
FROM [table_name]
WHERE [column_name] BETWEEN value1 AND value2;

Between Dates
SELECT [column_name]
FROM [table_name]
WHERE [column_name] BETWEEN #value1# AND #value2#;

Like and * wildcard
SELECT [column_name]
FROM [table_name]
WHERE [column_name] LIKE '*value*';

Count
SELECT COUNT([column_name])
FROM [table_name];

However in Aggregate queries if other columns are returned you must use Group By

SELECT COUNT([column_1]), [Column2]
FROM [table_name]
GROUP BY [Column2];

JOINS
SELECT [Table1].[Column_1], [Table2].[Column_2]
FROM Table1 INNER JOIN Table2
ON [Table1].[ID] = [Table2].[ID];

LEFT, RIGHT and INNER JOINS follow the same syntax.

Full outer joins are achieved by using no join as follows:

SELECT [Table1].[Column_1], [Table2].[Column_2]
FROM Table1, Table2;

Union
SELECT [column_name] FROM [table_1]
UNION [ALL]
SELECT [column_name] FROM [table_2];

The ALL predicate is required if you don't want duplicate records to be dropped.
UNION on it's own has the effect of using the DISTINCT predicate in a SELECT clause.
Jan 12 '07 #1
4 52394
abolos
65 New Member
good job mmaccarthy. very useful SQL statements.
Aug 11 '07 #2
Lysander
344 Recognized Expert Contributor
Nice set of SQL statements.

Would it be worth adding the data manipulation SQL, ie DROP TABLE, CREATE TABLE etc
Aug 13 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Nice set of SQL statements.

Would it be worth adding the data manipulation SQL, ie DROP TABLE, CREATE TABLE etc
I think a separate thead of more advanced SQL statements would be appropriate. Could also include crosstabs.
Aug 13 '07 #4
sierra7
446 Recognized Expert Contributor
A very good summary. I keep Acc97 loaded because the Help there is much better than in Acc2000+

One added tip for the Append Statement;-

INSERT INTO [table_name] ([column1], [column2], [column3])
VALUES ('value1', #value2#, value3);

is to use single quotes around decimal numbers if the software is to be used in continental Europe (not UK) because they use a comma instead of a full-stop (period) as the decimal symbol. Thus 12.5 would be displayed as 12,5 in the French locale and so interpreted as two values by Access, which errors moaning about number of values not matching.

If writing for europe it is necessary to put a Format statement on dates "yyyy-mm-dd" to ensure they are stored correctly.
Sep 11 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2324
by: Marco Alting | last post by:
Hi, I'm still confused about my queries, I want to do something is ASP that is easily done in Access. I'll post the Access queries below as a reference. The main idea is that the queries depend...
7
17374
by: Dana Shields | last post by:
I am attempting to upsize from access to SQL Server. I'm trying to convert my queries to SQL Server views; however, I'm having a lot of difficulty with the syntax differences. For instance, a...
1
1800
by: Craig Washington | last post by:
How in code can you use the Microsoft Access Queries with ADO in code? Anyone???
3
4160
by: Kevin Forbes | last post by:
So, I've found how to list all the tables in an Access database (as seen below) and running MS Access queries is easy (similar to executing a stored procedure), but how do I list the names of all...
3
3567
by: blackdevil1979 | last post by:
Hi, Anyone here familiar with crystal syntax or basic syntax. I need some help....
6
9491
by: jfbevilaqua | last post by:
Does anyone know of a utility or a method (VBA or otherwise) to be able to export ALL 100+ MS Access Queries from an Access Table into individual text files perhaps ending with .sql so they can be...
2
1982
by: MyEmailList | last post by:
We have an Access data base with severl tables, queries and forms. We want to put it on the web. Is there a tool that will convert the Access queries and forms to ASP? Sorta like "splitting"...
5
3983
by: forrestgump | last post by:
I want to be able to run multiple access queries on a single microsoft excel worksheet. Ideally I want to define what queries i want in excel and import the results of all queries one after...
3
1996
by: Shiny Star | last post by:
I have 4tables & I write class for all those table to hold a tables information as one whole unit. I am using MS Access queries, just like stored procedures so the queries (select, update, delete,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.