Courses/SQL/Cross-Database Queries

    Lesson 45 • Advanced

    Cross-Database Queries & Foreign Data Wrappers

    Query remote databases, CSV files, and external systems using Foreign Data Wrappers and linked servers.

    ✅ What You'll Learn

    • • PostgreSQL Foreign Data Wrappers (FDW)
    • • SQL Server Linked Servers and 4-part naming
    • • Querying CSV files with file_fdw
    • • Cross-database JOIN and performance considerations

    🌐 Why Cross-Database Queries?

    🎯 Real-World Analogy: Imagine you work at a company with offices in London, New York, and Tokyo. Each office has its own filing cabinet. Cross-database queries let you search all three filing cabinets from your desk in London — without flying to each office.

    In real systems, data lives in different databases: orders in PostgreSQL, analytics in ClickHouse, logs in Elasticsearch, user data in MySQL. FDWs let you JOIN across all of them with standard SQL.

    ⚠️ Performance Warning: Cross-database queries pull data over the network. Always filter early (push WHERE clauses to the remote side) and avoid pulling millions of rows across databases.

    Try It: PostgreSQL Foreign Data Wrappers

    Set up and query a remote PostgreSQL database as local tables

    Try it Yourself »
    SQL
    -- PostgreSQL Foreign Data Wrappers (FDW)
    -- Query remote databases as if they were local tables
    
    -- Step 1: Install the extension
    CREATE EXTENSION IF NOT EXISTS postgres_fdw;
    
    -- Step 2: Create a foreign server
    CREATE SERVER remote_analytics
        FOREIGN DATA WRAPPER postgres_fdw
        OPTIONS (host 'analytics-db.example.com',
                 port '5432',
                 dbname 'analytics');
    
    -- Step 3: Map local user to remote user
    CREATE USER MAPPING FOR current_user
        SERVER remote_analytics
        OP
    ...

    Try It: Query CSV Files with SQL

    Use file_fdw to query CSV files directly with SQL statements

    Try it Yourself »
    SQL
    -- Foreign Data Wrappers for files (CSV, JSON)
    -- Query CSV files directly with SQL!
    
    CREATE EXTENSION IF NOT EXISTS file_fdw;
    
    CREATE SERVER csv_files FOREIGN DATA WRAPPER file_fdw;
    
    -- Create a foreign table pointing to a CSV file
    CREATE FOREIGN TABLE import_log (
        timestamp TEXT,
        level TEXT,
        message TEXT,
        source TEXT
    ) SERVER csv_files
    OPTIONS (
        filename '/var/log/app/import.csv',
        format 'csv',
        header 'true',
        delimiter ','
    );
    
    -- Query the CSV like a regular tab
    ...

    📋 Quick Reference

    MethodDatabaseBest For
    postgres_fdwPostgreSQLRemote PostgreSQL tables
    Linked ServersSQL ServerCross-instance queries
    dblinkPostgreSQLAd-hoc remote queries
    file_fdwPostgreSQLCSV/log file queries
    FEDERATEDMySQLRemote MySQL tables

    🎉 Lesson Complete!

    You can now query across databases and file systems! Next, learn about performance testing and benchmarking.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service