DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Which Tool Is Better for Code Completion — Azure Data Studio or dbForge SQL Complete?
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • How to Store Text in PostgreSQL: Tips, Tricks, and Traps
  • Anatomy of a PostgreSQL Query Plan

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • System Coexistence: Bridging Legacy and Modern Architecture
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  1. DZone
  2. Data Engineering
  3. Databases
  4. Fun With SQL Using Postgres and Azure Data Studio

Fun With SQL Using Postgres and Azure Data Studio

In this article, I will show you some basic SQL when working with Postgres and Azure data studio.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Oct. 26, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

I have already written a few posts about PostgreSQL and this time I would like to take a step back and discuss some of the basics and at the same time share with you my experience of using Azure Data Studio.

Azure Data Studio is a cross-platform database tool for data professionals using the Microsoft family of on-premises and cloud data platforms on Windows, macOS, and Linux. It is very easy to install and offers a modern editor experience with IntelliSense, code snippets, source control integration, and an integrated terminal. It’s engineered with the data platform user in mind, with the built-in charting of query result sets and customizable dashboards. You can learn more about it from the official website on this link.

ADS also has notebooks that are similar to Jupiter notebooks for python and other languages and are great for combining formatted text with code. You can execute queries via a query window or via a notebook window. I will be using both of those. If you haven’t used ADS before, give it a try and you will like it.

Setup

I have installed the PostgreSQL database on my machine and I also have installed Azure Data Studio which I will use to execute various queries for the database. You can also use pgAdmin or any other tool as u wish. The following screenshot shows that I am connected to Postgres server.

showing connection to Postgres server

Create a Database

Ok, let's start something simple and create a database. I use the notebook window from the Azure Data Studio (ADS) and execute the following SQL which created the database as intended.

creating the database

and here is the database:

example database


Basic SQL Queries (Create, Alter, Select and Drop Basics)

In this section, we will refresh some basic SQL knowledge and some queries and their purposes.

Creating a Table

in DDL (Data Definition Language) SQL is very simple in PostgreSQL. Here is how the typical SQL looks like.

creating a table


the keyword serial is PostgreSQL specific and it set up an auto-incrementing value and that is the typical way for the primary-key. 

Deleting a Table

This is also very straight forward: Try not to do that in your production database :)

deleting table


 The if exists set up guards against error if the table doesn’t exist.

Altering a Table

is also very simple. e.g. if we want to alter a column in our table, the following is the syntax.

altering a table

Another example showing renaming a column:

renaming table

Insert Data

Let's insert and select some data using SQL.

inserting data

Select Data From a Table

The following query shows the result and we can see that registeredat column has a default value as well.

selecting data from table

Select, Limit, and Page Results

To limit the result, Postgres uses limit as shown below (SQL-server uses TOP keyword):

limting results

The other keyword which is very helpful and normally goes with limit is the keyword offset and used for pagination purposes: 

the above query tells Postgres to grab the 3 users but skip the first 2 essentially making it a paging query. 

Creating a Simple Function

Postgres has a lot of functions which we can use. We used now() function earlier which returns the current time. Another built-in function is random() which returns some random value as shown below:

creating a simple function


There are many functions available out of the box and we can create custom functions as well. Let's create a simple custom function using SQL. 

custom functions

executing this SQL will result in function creation and we can use this function as per our needs.

public current time

and here is how we can use it e.g. as a default value for a table column:

current time column

Summary

So, this was a very basic introduction to SQL, Postgres, and Azure Data Studio. Let me know if something is not clear. Till next time, happy coding.

References

  • https://docs.microsoft.com/en-us/sql/azure-data-studio/quickstart-postgres?view=sql-server-ver15
  • https://hexquote.com/net-core-postgresql-and-document-database/
sql Data (computing) PostgreSQL Database azure

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Which Tool Is Better for Code Completion — Azure Data Studio or dbForge SQL Complete?
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • How to Store Text in PostgreSQL: Tips, Tricks, and Traps
  • Anatomy of a PostgreSQL Query Plan

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!