# IDs in Data Explorer

**URL:** https://meta.discourse.org/t/ids-in-data-explorer/224775
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [April 19, 2022, 6:02pm UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775 "2022-04-19T18:02:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ganncamp](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ganncamp/32/106199_2.png) [@ganncamp](https://meta.discourse.org/u/ganncamp)
#### Post date: [April 19, 2022, 6:02pm UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775/1 "2022-04-19T18:02:51Z")

</div>

I’ve noticed that if I select an FK ID in a table, I get a beautifully presented, linked name. Whereas if I select a PK ID in a table, I just get a number.

For example, when I select the (PK) id field from Posts:

 ![Selection_130](https://global.discourse-cdn.com/meta/original/4X/1/1/9/1197d0d931ff6196f07d1006dbac539320f9ac6b.png)

Versus when I select the (FK) topic\_id field from Posts:

 ![Selection_129](https://global.discourse-cdn.com/meta/original/4X/7/0/e/70e5614dcec26507318045e22ee04cf53577769b.png)

I’ve been wondering if there’s some sleight-of-hand I can do to get this lovely linking behavior from PKs. Or do I always need to throw in an extra table & select from _it_ when I want linking?

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [April 19, 2022, 6:47pm UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775/2 "2022-04-19T18:47:00Z")

</div>

You can alias that particular `id` to `post_id` and it will work its magic. 👍 Eg.

```plaintext
SELECT id AS post_id
FROM posts 
ORDER BY created_at DESC

```

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [April 20, 2022, 2:52am UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775/3 "2022-04-20T02:52:28Z")

</div>

There are actually quite a few other little tricks to beautifying your results in the Discourse UI.

 ![Screen Shot 2022-04-19 at 7.45.42 PM](https://global.discourse-cdn.com/meta/original/4X/1/9/d/19df47d825ee7536ee108dc5dd3730c0de0e2d4c.png)

This old file shows some examples:

> <https://github.com/discourse/discourse-data-explorer/blob/9956b9833e223a0b459acc89056cc7546368241d/test/test_query.sql>

I’ll integrate this into the main [Data Explorer](https://meta.discourse.org/t/32566?silent=true) topic at some point when I have a spare moment.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [April 20, 2022, 6:15am UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775/4 "2022-04-20T06:15:29Z")

</div>

I also like the admin url one for making interactive user lists: 🙂

```plaintext
SELECT
  '/admin/users/' || users.id || '/' || users.username_lower
    AS admin_page_url
FROM users

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 17, 2022, 5:28pm UTC](https://meta.discourse.org/t/ids-in-data-explorer/224775/5 "2022-06-17T17:28:46Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
