473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Basic SQL Syntax for Access Queries

MMcCarthy
14,534 Expert Mod 8TB
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 52371
abolos
65
good job mmaccarthy. very useful SQL statements.
Aug 11 '07 #2
Lysander
344 Expert 100+
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 Expert Mod 8TB
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 Expert 256MB
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
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
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
by: Craig Washington | last post by:
How in code can you use the Microsoft Access Queries with ADO in code? Anyone???
3
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
by: blackdevil1979 | last post by:
Hi, Anyone here familiar with crystal syntax or basic syntax. I need some help....
6
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.