Distributed queries
- SQL course
- Expert
- Lesson 30/45
- 5 min
Run queries across multiple servers.
Distributed query
A distributed query pulls data from several databases with one SQL statement. In PostgreSQL a Foreign Data Wrapper (FDW) lets a remote table act like a local one.
CREATE FOREIGN TABLE remote_orders (id INT)
SERVER remote_server;A distributed query calls a remote table.
Distributed query — details
Federation is a layout where each node keeps its own data but a central query joins them all. A JOIN can run across remote and local tables.
Practice Distributed query
Create the remote_orders foreign table from remote_server: CREATE FOREIGN TABLE remote_orders (id INT) SERVER remote_server.
Starter code
-- distributed query
CREATE FOREIGN TABLE remote_ordersSample solution
CREATE FOREIGN TABLE remote_orders (id INT) SERVER remote_server;Expected output
CREATE FOREIGN TABLE
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a distributed query combine?
- An index
- One table
- One column
- Several databases Correct answer
Explanation: A distributed query combines several databases.
2 Create a foreign table
CREATE FOREIGN TABLE remote_orders (id INT) SERVER ____
CREATE FOREIGN TABLE remote_orders (id INT) SERVER remote_server
Explanation: remote_server is the right answer.
3 Distributed queries: match the terms
| Term | Meaning |
|---|---|
distributed |
distributed |
foreign |
foreign |
federation |
federation |
remote |
remote |